0

可能重复:
通过 Gmail 在 .NET 中发送电子邮件

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

 if (Session["userinfo"] != null)
     {

         lblTest.Text = Session["userinfo"].ToString();
         MailMessage msg = new MailMessage();
         msg.From = new MailAddress("shop.bcharya@gmail.com");
         msg.To.Add(new MailAddress("bcc@dr.com"));
         msg.To.Add(new MailAddress("info@yzentech.com"));
         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("shop.bcharya@gmail.com", "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);
         }

     }
4

4 回答 4

1

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

sc.Port = 587;

在这条线之后

sc.Host = "smtp.gmail.com";
于 2012-11-21T07:31:47.877 回答
1

如果 SMTP 服务器支持(例如 GMail 和 Hotmail),则仅使用端口 587 和 SSL。有些服务器只使用端口 25 而没有 SSL。

于 2012-11-21T08:35:46.473 回答
0

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

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(“yourmail@domain.com”, “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(“yourmail@domain.com”, “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;
 }
}
于 2012-11-21T09:55:44.170 回答
0

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

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

于 2012-11-21T07:59:49.123 回答