Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-10-03
12
Medium Priority
?
700 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:techques
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
12 Comments
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 22632309
You are trying to assign a string to an enum

replace

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

with

msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 22632446
this line:
msg.ReplyTo = to;

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

And REA_ANDREW already replied for the DeliveryNotificationOptions
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 22632459
oops missed that one. :-)

0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 

Author Comment

by:techques
ID: 22634557
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

0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 22634670
is your smtp server is really at "smtp.test.com" ?
0
 

Author Comment

by:techques
ID: 22634687
no, it is an example.

i use smtp server in outlook with username and password

0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 22635051
it is really hard to figure it out without the exact exception. have a look at http://www.systemnetmail.com/
0
 

Author Comment

by:techques
ID: 22645073
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

0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 22645228
have a look at "5 Troubleshooting System.Net.Mail" to trace all inner exceptions
0
 

Author Comment

by:techques
ID: 22647381
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?
0
 
LVL 20

Accepted Solution

by:
REA_ANDREW earned 375 total points
ID: 22647843
It is all to do with authentication.  You need to get all the ingredients correct to suit the SMTP Credentials of an Account you have with a provider.

If you cannot send on your home network, your details you are providing must be wrong.  BUT you have now been able to send you email using the System.Net.Mail through your friends network.  Which means we cannot really tell you why you cannot send on your home network as it is a bit beyond the scope of this topic area.  Or rather I could not tell you as I would have no idea what credentials you should use are.

If you can send email using an email client such as outlook from your home machine, then ring up your ISP and ask them for your SMTP details or query the account inside Microsoft Outlook Accounts.

Cheers,

Andrew

0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 22648423
>>If I use home network to send email, it does not work.

Your SMTP server is not set/installed correctly
0

Featured Post

Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question