Send Email using ASP.NET by GoDaddy mail server
After transferring to GoDaddy from another Hosting plan, one of the major changes I have to make is the email function.
Luckily I have all my email functionality in one utility class.
In this class I attempt to use both methods to send email. Actually both should work.
private static void SendEmailOut(string sTo, string sSubject, string strMessageText)
{
try
{
SendMailByWebMail(sSubject, sTo, strMessageText);
}
catch (Exception ee)
{
try
{
SendEmailByNetMail(sSubject, sTo, strMessageText);
}
catch (Exception ee2)
{
throw (ee2);
}
}
}
//method 1 Webmail
public static System.Web.Mail.MailMessage CreateMailMessage()
{
System.Web.Mail.MailMessage newMessage = new System.Web.Mail.MailMessage();
newMessage.From = "Technique Support";
newMessage.Headers.Add("Reply-To", "admin@mydomain.com");
newMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
newMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "admin@mydomain.com");
newMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "password");
newMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "80");
System.Web.Mail.SmtpMail.SmtpServer = "smtpout.secureserver.net";
newMessage.BodyFormat = System.Web.Mail.MailFormat.Html;
return newMessage;
}
private static void SendMailByWebMail(string sSubject, string sTo, string sBody)
{
System.Web.Mail.MailMessage newMessage = EmailService.CreateMailMessage();
newMessage.Subject = sSubject;
newMessage.To = sTo;
newMessage.Body = sBody;
System.Web.Mail.SmtpMail.Send(newMessage);
}
//method 2: using net mail
public static System.Net.Mail.SmtpClient GetNetMailServer()
{
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtpout.secureserver.net", 80);
smtp.Credentials = new System.Net.NetworkCredential("admin@mydomain.com", "password");
return smtp;
}
private static void SendEmailByNetMail(string sSubject, string sTo, string sBody)
{
System.Net.Mail.MailMessage EmailMsg = new System.Net.Mail.MailMessage("admin@mydomain.com", sTo);
EmailMsg.From = new System.Net.Mail.MailAddress("mydomain Support");
EmailMsg.Headers.Add("Reply-To", "admin@mydomain.com");
EmailMsg.Subject = sSubject;
EmailMsg.IsBodyHtml = true;
EmailMsg.Body = sBody;
EmailService.GetNetMailServer().Send(EmailMsg);
}
Both methods specifies the mail server and SMTP port, and account credential as required.
By the way, the Norton personal firewall does not block any of the above methods.
Luckily I have all my email functionality in one utility class.
In this class I attempt to use both methods to send email. Actually both should work.
private static void SendEmailOut(string sTo, string sSubject, string strMessageText)
{
try
{
SendMailByWebMail(sSubject, sTo, strMessageText);
}
catch (Exception ee)
{
try
{
SendEmailByNetMail(sSubject, sTo, strMessageText);
}
catch (Exception ee2)
{
throw (ee2);
}
}
}
//method 1 Webmail
public static System.Web.Mail.MailMessage CreateMailMessage()
{
System.Web.Mail.MailMessage newMessage = new System.Web.Mail.MailMessage();
newMessage.From = "Technique Support
newMessage.Headers.Add("Reply-To", "admin@mydomain.com");
newMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
newMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "admin@mydomain.com");
newMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "password");
newMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "80");
System.Web.Mail.SmtpMail.SmtpServer = "smtpout.secureserver.net";
newMessage.BodyFormat = System.Web.Mail.MailFormat.Html;
return newMessage;
}
private static void SendMailByWebMail(string sSubject, string sTo, string sBody)
{
System.Web.Mail.MailMessage newMessage = EmailService.CreateMailMessage();
newMessage.Subject = sSubject;
newMessage.To = sTo;
newMessage.Body = sBody;
System.Web.Mail.SmtpMail.Send(newMessage);
}
//method 2: using net mail
public static System.Net.Mail.SmtpClient GetNetMailServer()
{
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtpout.secureserver.net", 80);
smtp.Credentials = new System.Net.NetworkCredential("admin@mydomain.com", "password");
return smtp;
}
private static void SendEmailByNetMail(string sSubject, string sTo, string sBody)
{
System.Net.Mail.MailMessage EmailMsg = new System.Net.Mail.MailMessage("admin@mydomain.com", sTo);
EmailMsg.From = new System.Net.Mail.MailAddress("mydomain Support
EmailMsg.Headers.Add("Reply-To", "admin@mydomain.com");
EmailMsg.Subject = sSubject;
EmailMsg.IsBodyHtml = true;
EmailMsg.Body = sBody;
EmailService.GetNetMailServer().Send(EmailMsg);
}
Both methods specifies the mail server and SMTP port, and account credential as required.
By the way, the Norton personal firewall does not block any of the above methods.
Comments
Post a Comment