使用C#发送电子邮件

时间:2014-03-24 10:04:12

标签: c# mailmessage

下面显示的代码是我尝试发送电子邮件的方式。但是我收到错误Failure sending mail

有人能说出这里发生了什么问题吗?

            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("Smtp1.ajmanchamber.ae");
            mail.From = new MailAddress("Coo-services@ajmanchamber.ae");
            mail.To.Add(mailId);
            mail.Subject = "New COO Request Created";
            mail.Body = "New COO Request is created and your Reference Number is " + ReferenceNo;
            SmtpServer.Port = 587;
            SmtpServer.Credentials = new NetworkCredential("user", "pasword");
            SmtpServer.EnableSsl = true;
            SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
            SmtpServer.Send(mail);

当我更改代码并以这种方式尝试时,我得到了

            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("Smtp1.ajmanchamber.ae");
            mail.From = new MailAddress("Coo-services@ajmanchamber.ae");
            mail.To.Add(mailId);
            mail.Subject = "New COO Request Created";
            mail.Body = "New COO Request is created and your Reference Number is " + ReferenceNo;
            SmtpServer.Port = 25;
            SmtpServer.UseDefaultCredentials = false;
            SmtpServer.Credentials = new NetworkCredential("user", "password");
            SmtpServer.EnableSsl = true;
            SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
            SmtpServer.Send(mail);

我收到此错误根据验证程序,远程证书无效。

4 个答案:

答案 0 :(得分:1)

尝试这个

MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();

message.From = new MailAddress("EmailId");
message.To.Add(new MailAddress("ReceiverEmailId"));
message.Subject = "auto generated mail";
message.Body = "Hello world";
smtp.Port = 25;

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

smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("EmailId", "Password");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);

答案 1 :(得分:0)

mail.To.Add(mailId);替换为

mail.To = new MailAddress("example@domain.com");

我不明白您为何同时使用SmtpServer.UseDefaultCredentials = true;SmtpServer.Credentials = new NetworkCredential("user", "pasword");

答案 2 :(得分:0)

试试这个。

MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

mail.From = new MailAddress("me@mydomain.com");
mail.To.Add("u@urdomain.com");
mail.Subject = filename;
mail.Body = "Report";
Attachment attachment = new Attachment(filename);
mail.Attachments.Add(attachment);

SmtpServer.Port = 25;
SmtpServer.Credentials = new System.Net.NetworkCredential("me", "password");
SmtpServer.EnableSsl = true;

SmtpServer.Send(mail);

答案 3 :(得分:0)

您可以尝试这样

 MailMessage mail = new MailMessage();
 SmtpClient SmtpServer = new SmtpClient("Smtp1.ajmanchamber.ae");
 mail.From = new MailAddress("Coo-services@ajmanchamber.ae");
 mail.To.Add(mailId);
 mail.Subject = "New COO Request Created";
 mail.Body = "New COO Request is created and your Reference Number is " + ReferenceNo;
 SmtpServer.Port = 587;
 SmtpServer.UseDefaultCredentials = false;
 SmtpServer.Credentials = new NetworkCredential("user", "pasword");
 SmtpServer.EnableSsl = false;
 SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
 SmtpServer.Send(mail);  

如果你Enable Ssl,the Port is 465