使用c#[重复]在asp.net中发送邮件失败

时间:2022-08-14 18:15:18

Possible Duplicate:
Sending email in .NET through Gmail

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

This mail code is working in localhost i.e on my computer but when i uopload it on server it is not working. The error is : Failure sending mail. please tell me where is the problem.

这个邮件代码在localhost中工作,即在我的计算机上,但是当我在服务器上uopload它时,它无法正常工作。错误是:发送邮件失败。请告诉我问题出在哪里。

 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 个解决方案

#1


1  

For gmail mail settings add Port number too

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

sc.Port = 587;

after this line

在此之后

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

#2


1  

Only use port 587 and SSL if the SMTP server supports that (GMail and Hotmail for example). Some servers just use port 25 and no SSL.

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

#3


0  

Use below method and then check :

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

SmtpClient sc = new SmtpClient(string); //sends e-mail by using the specified SMTP server

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

#4


0  

You can use this below given code for sending email. Here sending the error details through email is one method. Try this code for sending email.

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

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;
 }
}

#1


1  

For gmail mail settings add Port number too

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

sc.Port = 587;

after this line

在此之后

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

#2


1  

Only use port 587 and SSL if the SMTP server supports that (GMail and Hotmail for example). Some servers just use port 25 and no SSL.

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

#3


0  

Use below method and then check :

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

SmtpClient sc = new SmtpClient(string); //sends e-mail by using the specified SMTP server

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

#4


0  

You can use this below given code for sending email. Here sending the error details through email is one method. Try this code for sending email.

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

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;
 }
}