Tuan Zaidi
Tuan Zaidi

Reputation: 343

Failed to send an EMail with body contains ip address and port no

I have create function to send an email. This function was work successful on localhost but on server its failed without any exception. I know the problem comes from my Port on IP Address.

The sample body is string body = "<p>Please click <a href=\"http://123.10.10.10:9200/User/Test?userId=24c04b9e-5a32-4b7c-a61e-ea3303dac679\">here</a></p>Thank You."

The problem is : between IP Address and Port.

Successful send an email if i remove :.

Do you guys have any ideas?

public void Sent(string sender, string receiver, string subject, string body)
    {
        using (MailMessage mail = new MailMessage(sender, receiver))
        {
            using (SmtpClient client = new SmtpClient())
            {
                client.Port = 25;
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.UseDefaultCredentials = false;
                client.Host = "mail.companyName.com.my";
                mail.Subject = subject;
                mail.IsBodyHtml = true;
                mail.Body = body;
                client.Send(mail);
            }
        }
    }

Upvotes: 1

Views: 221

Answers (2)

Cee McSharpface
Cee McSharpface

Reputation: 8725

You are doing it right, the code to send the mail is ok (you may want to revise the function name and make the smtp host name configurable, but that is not the point here).

The e-mail delivery fails on a relay, there is no immedieate feedback (no exception) to the client about this kind of failure.

The best bet is the IncreaseScoreWithRedirectToOtherPort property set in Set-HostedContentFilterPolicy in case your mail provider is Office365, or a similar spam filter mechanism in any other mail provider that is encountered down the mail delivery chain.

You can set a reply-to address and hope that the destination server will bounce a delivery failure that gives you more information. Or have the admin of the mail server look up the logs. More information here:
https://serverfault.com/questions/659861/office-365-exchange-online-any-way-to-block-false-url-spam

Upvotes: 1

Tiramonium
Tiramonium

Reputation: 867

Try setting the 'mail.Body' to receive a Raw Html message instead of a encoded string, like:

mail.Body = new System.Web.Mvc.HtmlHelper(new System.Web.Mvc.ViewContext(), new System.Web.Mvc.ViewPage()).Raw(body).ToString();

Or put a using System.Web.Mvc at the beginning so it gets shorter and easier to understand:

using System.Web.Mvc

mail.Body = new HtmlHelper(new ViewContext(), new ViewPage()).Raw(body).ToString();

Upvotes: 0

Related Questions