5

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

这就是我所拥有的,但它不起作用。我得到和异常说这是被禁止的。

            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 的问题,但我不知道。

4

5 回答 5

10

尝试这个:

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。

于 2013-07-31T06:10:37.510 回答
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);
于 2013-07-31T06:13:08.587 回答
1

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

SmtpClient MailClient = new SmtpClient("smtp.gmail.com",587);
于 2013-07-31T06:12:48.403 回答
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());
    }
于 2013-07-31T06:13:12.947 回答
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();
于 2017-06-21T13:51:24.370 回答