发送邮件SMTP,电子邮件提供商端口和主机名

时间:2013-04-19 16:10:16

标签: c# smtp

        String[] Sendto = File.ReadAllLines("SendTo.txt");
        SmtpClient X = new SmtpClient("smtp.gmail.com", 587);
        //X.UseDefaultCredentials = false;

        NetworkCredential Auth = new NetworkCredential("Someone@gmail.com", "Plainttext password");
        X.Credentials = Auth;
        X.EnableSsl = true;
        foreach (string recip in Sendto)
        {
            try
            {
                MailAddress from = new MailAddress("Someone@gmail.com");

                MailAddress to = new MailAddress(recip);
                MailMessage myMail = new MailMessage(from, to);
                myMail.Priority = MailPriority.High;
                myMail.Subject = "Your Test is Done";
                myMail.SubjectEncoding =
                myMail.BodyEncoding = System.Text.Encoding.UTF8;
                myMail.IsBodyHtml = true;
               X.Send(myMail);
            }
            catch (Exception ex)
            { Console.WriteLine(ex.Message); }

为什么这不起作用?我嗅到了流量,我从smpt.google.com得到了一个dns回复, 但无法连接到远程服务器

1 个答案:

答案 0 :(得分:0)

修改后的答案端口993没有工作......

对我有用的是: -

请注意,如果您有2步认证,则可能需要特定于应用的密码。

var mail = new MailMessage();

mail.From = new MailAddress("from address");
mail.To.Add("to address");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";

var smtpServer = new SmtpClient("smtp.gmail.com");
smtpServer.Port = 587;
smtpServer.Credentials = new System.Net.NetworkCredential("user", "password");
smtpServer.EnableSsl = true;
smtpServer.Send(mail);