使用c#在asp.net中发送邮件失败

sending mail failure in asp.net using c#

本问题已经有最佳答案,请猛点这里访问。

Possible Duplicate:
Sending email in .NET through Gmail

此邮件代码在本地主机(即我的计算机)中工作,但当我在服务器上加载它时,它不工作。错误是:发送邮件失败。请告诉我问题出在哪里。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 if (Session["userinfo"] != null)
     {

         lblTest.Text = Session["userinfo"].ToString();
         MailMessage msg = new MailMessage();
         msg.From = new MailAddress("[email protected]");
         msg.To.Add(new MailAddress("[email protected]"));
         msg.To.Add(new MailAddress("[email protected]"));
         msg.Subject ="Mail from BcharyaCorporation.online shopping site";
         msg.Body =""+lblTest.Text+"  wants to buy some products. please contact with him/her";
         SmtpClient sc = new SmtpClient();
         sc.Host ="smtp.gmail.com";
         // sc.Port = 25;
         sc.Credentials = new NetworkCredential("[email protected]","mypassword");
         sc.EnableSsl = true;
         try
         {
             sc.Send(msg);
             lblPayment.Text ="Sorry. Currently we are out of online payment service. We will contact you for payment process. Thank you for buying this product.";

         }
         catch (Exception ex)
         {
             lblPayment.Text=ex.Message.ToString();
             Response.Write(ex.Message);
         }

     }


仅当SMTP服务器支持时才使用端口587和SSL(例如gmail和hotmail)。有些服务器只使用端口25,不使用SSL。


对于gmail邮件设置,也添加端口号

1
sc.Port = 587;

在这条线之后

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


您可以使用下面给出的代码发送电子邮件。在这里,通过电子邮件发送错误详细信息是一种方法。尝试使用此代码发送电子邮件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System.Web.Mail
public static bool SendErrorEmail(string to, string cc, string bcc, string subject,    string body, MailPriority priority, bool isHtml)
{
 try
{
using (SmtpClient smtpClient = new SmtpClient())
{
using (MailMessage message = new MailMessage())
 {
 MailAddress fromAddress = new MailAddress("[email protected]","Your name");
 // You can specify the host name or ipaddress of your server
 smtpClient.Host ="mail.yourdomain.com"; //you can specify mail server IP address here
 //Default port is 25
 smtpClient.Port = 25;
 NetworkCredential info = new NetworkCredential("[email protected]","your password");
 smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
 smtpClient.UseDefaultCredentials = false;
 smtpClient.Credentials = info;
 //From address will be given as a MailAddress Object
 message.From = from;
 message.Priority = priority;
 // To address collection of MailAddress
 message.To.Add(to);
 message.Subject = subject;
 // CC and BCC optional
 if (cc.Length > 0)
 {
 message.CC.Add(cc);
 }
 if (bcc.Length > 0)
 {
 message.Bcc.Add(bcc);
 }
 //Body can be Html or text format;Specify true if it is html message
 message.IsBodyHtml = isHtml;
 // Message body content
 message.Body = body;
 // Send SMTP mail
 smtpClient.Send(message);
 }
 }
 return true;
 }
 catch (Exception ee)
 {
 Logger.LogError(ee,"Error while sending email to" + toAddress);
 throw;
 }
}


使用以下方法,然后检查:

smtpclient sc=new smtpclient(string);//使用指定的smtp服务器发送电子邮件