I have email sending problem through ASP.Net by the following class. Email goes fine on our own server but it doesn't send email on other servers especially yahoo and hotmail. If anybody helps whats the problem
//////////////email class/////////////////////
private void SendMail(string mailBody)
{
//Create message object and populate w/ data from form
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.From = new System.Net.Mail.MailAddress("webmaster@nust.edu.pk");
message.To.Add(txtemail.Text.Trim());
message.Subject = "UG Admission 2009";
message.Body = mailBody;
message.IsBodyHtml=true;
//Setup SmtpClient to send email. Uses web.config settings.
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
//Error handling for sending message
try
{
smtpClient.Send(message);
//Exception contains information on each failed receipient
}
catch (System.Net.Mail.SmtpFailedRecipientsException recExc)
{
for (int recipient = 0; recipient < recExc.InnerExceptions.Length - 1; recipient++)
{
System.Net.Mail.SmtpStatusCode statusCode;
//Each InnerException is an System.Net.Mail.SmtpFailed RecipientException
statusCode = recExc.InnerExceptions[recipient].StatusCode;
if ((statusCode == System.Net.Mail.SmtpStatusCode.MailboxBusy) || (statusCode == System.Net.Mail.SmtpStatusCode.MailboxUnavailable))
{
//Log this to event log: recExc.InnerExceptions[recipient].FailedRecipient
System.Threading.Thread.Sleep(5000);
smtpClient.Send(message);
}
else
{
//Log error to event log.
Response.Write(recExc.InnerExceptions[recipient].StatusCode);// or use statusCode);
}
}
}
//General SMTP execptions
catch (System.Net.Mail.SmtpException smtpExc)
{
//Log error to event log using StatusCode information in
// smtpExc.StatusCode;
}
catch (Exception ex)
{
//Log error to event log.
}
}
///////////////////end code/////////////////////////////
Thanks for your suggestion
Shakeel ur Rehman
I have email sending problem through ASP.Net by the following class. Email goes fine on our own server but it doesn't send email on other servers especially yahoo and hotmail. If anybody helps whats the problem
//////////////email class///////////////////// private void SendMail(string mailBody) {
//Create message object and populate w/ data from form System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); message.From = new System.Net.Mail.MailAddress("webmaster@nust.edu.pk");
message.To.Add(txtemail.Text.Trim()); message.Subject = "UG Admission 2009"; message.Body = mailBody; message.IsBodyHtml=true;
//Setup SmtpClient to send email. Uses web.config settings. System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
//Error handling for sending message try { smtpClient.Send(message); //Exception contains information on each failed receipient }
catch (System.Net.Mail.SmtpFailedRecipientsException recExc) { for (int recipient = 0; recipient < recExc.InnerExceptions.Length - 1; recipient++) { System.Net.Mail.SmtpStatusCode statusCode; //Each InnerException is an System.Net.Mail.SmtpFailed RecipientException statusCode = recExc.InnerExceptions[recipient].StatusCode;
if ((statusCode == System.Net.Mail.SmtpStatusCode.MailboxBusy) || (statusCode == System.Net.Mail.SmtpStatusCode.MailboxUnavailable)) { //Log this to event log: recExc.InnerExceptions[recipient].FailedRecipient System.Threading.Thread.Sleep(5000); smtpClient.Send(message); } else { //Log error to event log. Response.Write(recExc.InnerExceptions[recipient].StatusCode);// or use statusCode); }
} } //General SMTP execptions catch (System.Net.Mail.SmtpException smtpExc) { //Log error to event log using StatusCode information in // smtpExc.StatusCode; } catch (Exception ex) { //Log error to event log. } }
///////////////////end code/////////////////////////////
Thanks for your suggestion Shakeel ur Rehman
hi
please use this code
public bool SendMail()
{
try
{
string sPortNumber = ConfigurationManager.AppSettings["PortNumber"];//25 useport number 25
System.Web.Mail.MailMessage msg = new MailMessage();
msg.BodyFormat = System.Web.Mail.MailFormat.Html;
//System.Web.Mail.MailAttachment atchment = new MailAttachment("abc");
msg.To = to; //email;
msg.Cc = Cc;
msg.From = from;
msg.Subject = subject;
msg.Body = message;
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", sPortNumber);
int j;
if (m_attachments.Count > 0)
{
foreach (string strAttachment in m_attachments)
{
System.Web.Mail.MailAttachment atchment = new MailAttachment(strAttachment);
msg.Attachments.Add(atchment);
}
}
SmtpMail.SmtpServer = ConfigurationManager.AppSettings["SMTPServerName"].ToString();//Smtp Mail Server Name
SmtpMail.Send(msg);
}
catch(Exception ex)
{
try
{
bool bmsgSendFlag;
ExceptionBase objEx = new ExceptionBase();
bmsgSendFlag = objEx.LogErrorWithoutSendingMail(ex, this.ToString());
}
catch( Exception ex1)
{
}
msgSendFlag = false;
}
return msgSendFlag;
}