Link to home
Start Free TrialLog in
Avatar of techques
techques

asked on

How to configure the parameters in System.Net.Mail and send email?

Hi

I use System.Net.Mail to send email. But, it has error. The smtp ac does work when I use in outlook.

How should I set the parameters:

msg.ReplyTo = to;
msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure.ToString();

as it said the errors are not able to convert string to msg.ReplyTo and DeliveryNotificationOptions.


public void SendMailNow(string from, string to, string title, string content)
        {
            SmtpClient smtp = new SmtpClient(Host);
            if (IsUserCredentials)
            {
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = Credential;
            }
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            MailMessage msg = new MailMessage(from, to, title, content);
            msg.IsBodyHtml = true;
            msg.ReplyTo = to;
            msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure.ToString();
            msg.BodyEncoding = Encoding.GetEncoding(950); //936 - simplified chinese
            msg.SubjectEncoding = Encoding.GetEncoding(950); //950 - traditional chinese
            smtp.Send(msg);
        }

Open in new window

Avatar of REA_ANDREW
REA_ANDREW
Flag of United Kingdom of Great Britain and Northern Ireland image

You are trying to assign a string to an enum

replace

msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure.ToString();

with

msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
Avatar of Éric Moreau
this line:
msg.ReplyTo = to;

should read:
msg.ReplyTo = new MailAddress(to);

And REA_ANDREW already replied for the DeliveryNotificationOptions
oops missed that one. :-)

Avatar of techques
techques

ASKER

Hi

I edited the code and try again. It has an exception: -            
$exception      {"Failure sending mail."}      System.Exception {System.Net.Mail.SmtpException}

I use VC# to develop that windows service program.
What's wrong with it?

using System.Net.Mail;
public void SendMailNow(string from, string to, string title, string content)
        {
            SmtpClient smtp = new SmtpClient(Host);
            if (IsUserCredentials)
            {
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = Credential;
            }
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            MailMessage msg = new MailMessage(from, to, title, content);
            msg.IsBodyHtml = true;
            msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
            msg.BodyEncoding = Encoding.GetEncoding(950); //936 - simplified chinese
            msg.SubjectEncoding = Encoding.GetEncoding(950); //950 - traditional chinese
            smtp.Send(msg);
        }
 
public const string SmtpClientHostFrom = "a@test.com";
public const string SmtpClientHost = "smtp.test.com";
public const string SmtpClientIsUserCredentials = "true";
public const string SmtpClientIsOnlyForLocUser = "false";
public const string SmtpClientCredentialAccount = "admin@test.com";
public const string SmtpClientCredentialPassword = "1234";

Open in new window

is your smtp server is really at "smtp.test.com" ?
no, it is an example.

i use smtp server in outlook with username and password

it is really hard to figure it out without the exact exception. have a look at http://www.systemnetmail.com/
Hi

I read http://www.systemnetmail.com/allfaq.aspx and edited the code, it has the same error: Failure to send email.  

How can I fix it now?

public void SendMail(string to, string title, string content)   
        {   
            MailMessage msg = new MailMessage();   
            msg.From = new MailAddress("me@mycompany.com","My Company Name");   
            msg.To.Add(to);   
            msg.Subject = title;   
            msg.Body = content;   
            msg.IsBodyHtml = true;   
            msg.Priority = MailPriority.Normal;   
            msg.ReplyTo = new MailAddress("me@mycompany.com");   
            msg.Headers.Add("Disposition-Notification-To", "me@mycompany.com");   
            msg.SubjectEncoding = Encoding.GetEncoding(950);   
            msg.BodyEncoding = Encoding.GetEncoding(950);   
            msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;   
            SmtpClient smtp = new SmtpClient(smtp.sample.com);   
            smtp.Credentials = new NetworkCredential("testusr", "testpwd");   
            smtp.Send(msg);   
}   

Open in new window

have a look at "5 Troubleshooting System.Net.Mail" to trace all inner exceptions
If I use home network to send email, it does not work.

However, if I use my friend's network, it can send email.

Why is that case?
ASKER CERTIFIED SOLUTION
Avatar of REA_ANDREW
REA_ANDREW
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>If I use home network to send email, it does not work.

Your SMTP server is not set/installed correctly