Porting System.Web.Mail functionality to System.Net.Mail

4.1k views Asked by At

The following function works perfectly:

    protected void SendWebMailMessage(string DisplayName, string From, string ReplyTo,
        string To, string Subject, string Body, string Attachments)
    {
        System.Web.Mail.MailMessage msg = new System.Web.Mail.MailMessage();
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver",
             "smtpout.secureserver.net");
        msg.Fields.Add(
             "http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25);
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", 
             2);
        msg.Fields.Add(
             "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
        msg.Fields.Add(
             "http://schemas.microsoft.com/cdo/configuration/sendusername", From);
        msg.Fields.Add(
             "http://schemas.microsoft.com/cdo/configuration/sendpassword",
             mailpassword);
        msg.To = To;
        msg.From = DisplayName + "<" + From + ">";
        msg.BodyFormat = MailFormat.Html;
        msg.Subject = Subject;
        msg.Body = Body;
        msg.Headers.Add("Reply-To", ReplyTo);
        SmtpMail.SmtpServer = "smtpout.secureserver.net";
        SmtpMail.Send(msg);
    }

However, I get a build warning telling me that System.Web.Mail is obsolete, and that I should be using System.Net.Mail. So I used System.Net.Mail, and I came up with the following function to replace my old one:

    protected void SendNetMailMessage(string DisplayName, string From, string ReplyTo, 
        string To, string Subject, string Body, string Attachments)
    {
        MailAddress addrfrom = new MailAddress(From, DisplayName);
        MailAddress addrto = new MailAddress(To);
        MailAddress replytoaddr = new MailAddress(ReplyTo);
        System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
        msg.From = addrfrom;
        msg.To.Add(addrto);
        msg.ReplyTo = replytoaddr;
        msg.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient("smtpout.secureserver.net");
        smtp.Credentials = new NetworkCredential(From, mailpassword);
        smtp.Send(msg);
    }

I don't get any exceptions or errors, but my message never goes through. Can anyone tell me what I might be doing wrong? Thanks in advance.

2

There are 2 answers

0
Romias On BEST ANSWER

If I'm not wrong... you are using the Goddady hosting. I'm also using it... and I have **relay-hosting.secureserver.net** as the SMTP server. I don't know... may be can be something like that... or you have not set the Relay in your console.

0
bstoney On

I tested you code above an it works fine. I had to add the body and subject properties, but even without them I still received a blank email. I also tried with and without providing credentials, both worked.

Initially I hadn't changed your host string and did receive an exception with the message "Failure sending mail."