使用MailMessage在C#中发送电子邮件

时间:2013-07-31 06:05:16

标签: c# mailmessage

我在使用MailMessage发送电子邮件时遇到一些问题。我有两个电子邮件帐户(account1@gmail.com和account2@gmail.com),我希望account2能够通过按钮点击事件向帐户1发送电子邮件。

这就是我所拥有的,但它不起作用。我得到了例外情况并说这是禁止的。

            try
            {
                //do submit
                MailMessage emailMessage = new MailMessage();
                emailMessage.From = new MailAddress("account2@gmail.com", "Account2");
                emailMessage.To.Add(new MailAddress("account1@gmail.com", "Account1"));
                emailMessage.Subject = "SUBJECT";
                emailMessage.Body = "BODY";
                emailMessage.Priority = MailPriority.Normal;
                SmtpClient MailClient = new SmtpClient("smtp.gmail.com");
                MailClient.Credentials = new System.Net.NetworkCredential("account2@gmail.com", "password");
                MailClient.Send(emailMessage);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

我觉得这是Smtp的一个问题,但我不知道。

5 个答案:

答案 0 :(得分:8)

试试这个:

using (MailMessage emailMessage = new MailMessage())
{
    emailMessage.From = new MailAddress("account2@gmail.com", "Account2");
    emailMessage.To.Add(new MailAddress("account1@gmail.com", "Account1"));
    emailMessage.Subject = "SUBJECT";
    emailMessage.Body = "BODY";
    emailMessage.Priority = MailPriority.Normal;
    using (SmtpClient MailClient = new SmtpClient("smtp.gmail.com", 587))
    {
        MailClient.EnableSsl = true;
        MailClient.Credentials = new System.Net.NetworkCredential("account2@gmail.com", "password");
        MailClient.Send(emailMessage);
    }                               
} 

我将端口添加到smtp客户端并启用了SSL。 如果端口587不起作用,请尝试端口465。

答案 1 :(得分:2)

<强>尝试:

MailMessage Msg = new MailMessage();

Msg.From = new MailAddress(txtUsername.Text);

Msg.To.Add(txtTo.Text);

Msg.Subject = txtSubject.Text;

Msg.Body = txtBody.Text;

SmtpClient smtp = new SmtpClient();

smtp.Host = "smtp.gmail.com";

smtp.Port = 587;

smtp.Credentials=new System.Net.NetworkCredential(txtUsername.Text,txtpwd.Text);

smtp.EnableSsl = true;

smtp.Send(Msg);

答案 2 :(得分:1)

您尚未定义端口号。它应该是587。并使用enableSsl=true 如下:

SmtpClient MailClient = new SmtpClient("smtp.gmail.com",587);

答案 3 :(得分:1)

尝试添加端口号587并启用SSL on,因为Gmail使用“严格”的SSL安全性。这意味着我们将始终强制您的其他提供商的远程服务器具有有效的SSL证书。:

    try
    {
        //do submit
        MailMessage emailMessage = new MailMessage();
        emailMessage.From = new MailAddress("account2@gmail.com", "Account2");
        emailMessage.To.Add(new MailAddress("account1@gmail.com", "Account1"));
        emailMessage.Subject = "SUBJECT";
        emailMessage.Body = "BODY";
        emailMessage.Priority = MailPriority.Normal;
        SmtpClient MailClient = new SmtpClient("smtp.gmail.com", 587);
        MailClient.Credentials = new System.Net.NetworkCredential("account2@gmail.com", "password");
        MailClient.EnableSsl = true;
        MailClient.Send(emailMessage);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }

答案 4 :(得分:0)

MailMessage objMail = new MailMessage();
SmtpClient objSMTP = new SmtpClient("from.google.uk");
MailAddress objTo = new MailAddress("e-mail", "name");

string sql = "Select Naam, Mail from tblMail";

OleDbCommand cmdMail = new OleDbCommand(sql, cnnConnectie);

OleDbDataReader dtrMail = default(OleDbDataReader);

cnnConnectie.Open();
dtrMail = cmdMail.ExecuteReader();

while (dtrMail.Read())
{
    objMail.To.Add(dtrMail["Mail"].ToString());

    objMail.From = objTo;
    objMail.Body = "test";
    objMail.Subject = dtrMail["name"].ToString();
    objSMTP.Send(objMail);
}
cnnConnectie.Close();