Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过SMTP服务器以编程方式发送电子邮件_C#_Smtp - Fatal编程技术网

C# 通过SMTP服务器以编程方式发送电子邮件

C# 通过SMTP服务器以编程方式发送电子邮件,c#,smtp,C#,Smtp,试图通过SMTP服务器发送电子邮件,但我在SMTP.send(mail)上收到一个无法描述的错误代码片段 服务器端,中继IP地址看起来正确。为我错过的东西挠头 MailMessage mail = new MailMessage(); mail.To.Add(txtEmail.Text); mail.From = new MailAddress("no-reply@company.us"); mail.Subject = "Thank you for your submission...";

试图通过SMTP服务器发送电子邮件,但我在
SMTP.send(mail)上收到一个无法描述的错误代码片段

服务器端,中继IP地址看起来正确。为我错过的东西挠头

MailMessage mail = new MailMessage();
mail.To.Add(txtEmail.Text);

mail.From = new MailAddress("no-reply@company.us");
mail.Subject = "Thank you for your submission...";
mail.Body = "This is where the body text goes";
mail.IsBodyHtml = false;

SmtpClient smtp = new SmtpClient();
smtp.Host = "mailex.company.us";
smtp.Credentials = new System.Net.NetworkCredential
     ("AdminName", "************");

smtp.EnableSsl = false;


if (fileuploadResume.HasFile)
{
    mail.Attachments.Add(new Attachment(fileuploadResume.PostedFile.InputStream,
        fileuploadResume.FileName));
}

smtp.Send(mail);

尝试添加
SmtpDeliveryMethod=SmtpDeliveryMethod.Network发送前

以下是我的标准邮件功能,仅供参考:

public void sendMail(MailMessage msg)
{
    string username = "username";  //email address or domain user for exchange authentication
    string password = "password";  //password
    SmtpClient mClient = new SmtpClient();
    mClient.Host = "mailex.company.us";
    mClient.Credentials = new NetworkCredential(username, password);
    mClient.DeliveryMethod = SmtpDeliveryMethod.Network;
    mClient.Timeout = 100000;
    mClient.Send(msg);
}
通常被称为这样的东西:

MailMessage msg = new MailMessage();
msg.From = new MailAddress("fromAddr");
msg.To.Add(anAddr);

if (File.Exists(fullExportPath))
{
    Attachment mailAttachment = new Attachment(fullExportPath); //attach
    msg.Attachments.Add(mailAttachment);
    msg.Subject = "Subj";
    msg.IsBodyHtml = true;
    msg.BodyEncoding = Encoding.ASCII;
    msg.Body = "Body";
    sendMail(msg);
}
else
{
    //handle missing attachments
}

这是阿迪尔的答案,格式为C#:


您没有指定端口

smtp.Port = 1111; // whatever port your SMTP server uses

SMTP有三个不同的“标准”端口:25465和587。根据msdn文档,端口属性的默认值为25。

txtEmail看起来像什么?您是否尝试在其中硬编码电子邮件地址以查看其是否发送?我假设您已在配置文件中设置了正确的SMTP设置?您必须在代码“SMTP.UseDefaultCredentials=false;”或在web.config邮件设置中指定。另外,您指定的端口是什么?什么是“不可描述的错误?”发布堆栈跟踪(确保编辑掉任何机密数据)。下载并手动尝试以确保服务器没有问题。通过Telnet在端口25连接到服务器
mailex.company.us
。给你。
public static class Email
{
    private static string _senderEmailAddress = "sendermailadress";
    private static string _senderPassword = "senderpassword";

    public static void SendEmail(string receiverEmailAddress, string subject, string body)
    {
        try
        {
            var client = new SmtpClient("smtp.gmail.com", 587)
            {
                Credentials = new NetworkCredential(_senderEmailAddress, _senderPassword),
                EnableSsl = true
            };
            client.Send(_senderEmailAddress, receiverEmailAddress, subject, body);
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception sending email." + Environment.NewLine + e);
        }
    }
}
smtp.Port = 1111; // whatever port your SMTP server uses