Sending an email programmatically through an SMTP

2019-04-10 19:35发布

问题:

Trying to send an email through an SMTP server, but I'm getting a nondescript error on the smtp.Send(mail); snippet of code.

Server side, the relay IP addresses look correct. 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);

回答1:

Try adding 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
}


回答2:

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


回答3:

This is Adil's answer formatted for 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);
        }
    }
}


回答4:

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.



标签: c# smtp