伙计们我正在尝试使用ASP.net中的以下代码发送电子邮件,但它无法正常工作。但是,相同的代码适用于win表单。我正在使用c#。什么似乎是问题?
try
{
using (System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage())
{
message.To.Add("//some id");
message.Subject = "New Ticket Generated";
message.From = new System.Net.Mail.MailAddress("//id");
message.IsBodyHtml = true;
message.Body = "This is message body";
using (System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient())
{
smtp.Host = "// smtp host";
smtp.Credentials = new System.Net.NetworkCredential("// username", "// pass");
smtp.Send(message);
}
}
}
catch { }
答案 0 :(得分:1)
我认为你已经错过了一些编写代码来编写asp.net中的代码
codes are
smtp.Port = "port number";
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
以下是一些可能对您有帮助的代码
try
{
using (System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage())
{
message.To.Add("to emailadress");
message.Subject = "New Ticket Generated";
message.From = new System.Net.Mail.MailAddress("from emailaddress");
message.IsBodyHtml = true;
message.Body = "This is message body";
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 25;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("yourEmailid", "yourpassword");
smtp.Send(message);
}
}
catch(Exception)
{
throw;
}
N.B-这里我正在使用gmail smtp服务器和端口,因为你必须使用电子邮件地址"来自emailaddress"并且在smtp.Credentials = new System.Net.NetworkCredential("来自emailaddress的相同电子邮件ID","来自emailaddress的密码");
例如
message.From = new System.Net.Mail.MailAddress("rahul@gmail.com");
smtp.Credentials = new System.Net.NetworkCredential("rahul@gmail.com", "myp@ssword");
按照此链接获取所有smtp服务器详细信息的参考 http://www.arclab.com/en/amlc/list-of-smtp-and-pop3-servers-mailserver-list.html
试试吧,祝你好运
答案 1 :(得分:0)
检查以下内容:
请参阅文档
http://msdn.microsoft.com/en-us/library/swas0fwc%28v=vs.110%29.aspx
并查看SmtpClient.Send()方法可以抛出的所有可能异常。更有可能的是,我认为它被抛出的SmtpException或者不太可能是SmtpFailedRecipientsException。