简体   繁体   English

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

[英]Sending an email programmatically through an SMTP server

Trying to send an email through an SMTP server, but I'm getting a nondescript error on the smtp.Send(mail); 试图通过SMTP服务器发送电子邮件,但我在smtp.Send(mail);上收到一个smtp.Send(mail);错误smtp.Send(mail); snippet of code. 代码片段。

Server side, the relay IP addresses look correct. 服务器端,中继IP地址看起来正确。 Scratching my head about what I'm missing. 我不知道自己错过了什么。

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);

Try adding smtp.DeliveryMethod = SmtpDeliveryMethod.Network; 尝试添加smtp.DeliveryMethod = SmtpDeliveryMethod.Network; prior to send. 在发送之前。

For reference, here is my standard mail function: 作为参考,这是我的标准邮件功能:

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);
}

Typically called something like this: 通常称为这样的事情:

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
}
            var client = new SmtpClient("smtp.gmail.com", 587);
            Credentials = new NetworkCredential("sendermailadress", "senderpassword"),
            EnableSsl = true
            client.Send("sender mail adress","receiver mail adress", "subject", "body");
            MessageBox.Show("mail sent");

This is Adil's answer formatted for C#: 这是Adil针对C#格式化的答案:

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);
        }
    }
}

You didn't specify the port. 您没有指定端口。

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

The SMTP has three different "standard" ports: 25, 465 and 587. According to msdn documentation, the default value for the Port property is 25. SMTP有三个不同的“标准”端口:25,465和587.根据msdn文档,Port属性的默认值为25。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM