Solved

SMTP e MailSetting in web.config

Posted on 2010-08-23
13
1,333 Views
Last Modified: 2012-06-27
Hi!

I have an ASP.net website that sends emails to users.

I have this configuration on web.config:

<system.net>
    <mailSettings>
      <smtp from="_______">
        <network host="smtpout.europe.secureserver.net" port="3535" userName="______" password="**********" defaultCredentials="true" />
      </smtp>
    </mailSettings>
</system.net>

Open in new window


In C# I have to set the configurations again:

SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtpout.europe.secureserver.net";
            System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
            NetworkCred.UserName = "_____";
            NetworkCred.Password = "*******";
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = 3535;

                smtp.Send(MailMessage);

Open in new window


It does works, but I want to set the configuration only in web.config.
How can I use the configurations from web.config ?

Thanks in advance!
0
Comment
Question by:calypsoworld
[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
  • 6
  • 6
13 Comments
 
LVL 8

Expert Comment

by:RyanAndres
ID: 33505747
You just set it in the <mailSettings> and don't need to reassigned the smtp server values...

See Scott Gu's post: http://weblogs.asp.net/scottgu/archive/2005/12/10/432854.aspx
MailMessage message = new MailMessage();
message.From = new MailAddress("sender@foo.bar.com");
 
message.To.Add(new MailAddress("recipient1@foo.bar.com"));
message.To.Add(new MailAddress("recipient2@foo.bar.com"));
message.To.Add(new MailAddress("recipient3@foo.bar.com"));
 
message.CC.Add(new MailAddress("carboncopy@foo.bar.com"));
message.Subject = "This is my subject";
message.Body = "This is the content";
 
SmtpClient client = new SmtpClient();
client.Send(message);

Open in new window

0
 

Author Comment

by:calypsoworld
ID: 33506000
Thank you for reply!

When I try to send an email without setting the smtp values in C#, I get this error:

Mailbox name not allowed. The server response was: Sorry, that domain isn't in my list of allowed rcpthosts.
0
 
LVL 8

Expert Comment

by:RyanAndres
ID: 33506029
That means the code works fine, but your smtp server is restricting the email address in the To field. You should check that you have permissions on the smtp server.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:calypsoworld
ID: 33506060
But why it does works if I set the values in C# and does not if uses the values from web.config?

Thank you!
0
 
LVL 8

Expert Comment

by:RyanAndres
ID: 33506163
Could you show your config and cod? Do mask any sensitive data.
0
 

Author Comment

by:calypsoworld
ID: 33506471
The entire web.config and the method that sends the email ?
0
 
LVL 8

Expert Comment

by:RyanAndres
ID: 33506487
No just the parts that are relevant. The mailSettings and wherever SmtpClient and MailMessage is usde.
0
 

Author Comment

by:calypsoworld
ID: 33506640
public string sendEmailContactForm(string email, string name, string sub, string msg1, string msg2)
        {
            MailMessage mailToAdmin = new MailMessage();
            mailToAdmin.From = new MailAddress("Portal 123<123@123.info>");
            mailToAdmin.To.Add(new MailAddress("123@123.info"));
            mailToAdmin.Subject =sub;
            mailToAdmin.Body = msg1;
            mailToAdmin.IsBodyHtml = true;

            MailMessage mailToUser = new MailMessage();
            mailToUser.From = new MailAddress("Portal 123<123@123.info>");
            mailToUser.To.Add(new MailAddress(email));
            mailToUser.Subject = sub;
            mailToUser.Body = msg2;
            mailToUser.IsBodyHtml = true;

            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtpout.europe.secureserver.net";
            System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
            NetworkCred.UserName = "123@123.info";
            NetworkCred.Password = "*****";
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = 3535;
            try
            {
                smtp.Send(mailToUser);
                smtp.Send(mailToAdmin);
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }


///////////////////////


<system.net>
    <mailSettings>
      <smtp from="123@123.info">
        <network host="smtpout.europe.secureserver.net" port="3535" userName="123@123.info" password="*****" defaultCredentials="true" />
      </smtp>
     </mailSettings>
</system.net>
0
 
LVL 8

Accepted Solution

by:
RyanAndres earned 500 total points
ID: 33506688
Try keeping it to the bare minimum. See code:
public string sendEmailContactForm(string email, string name, string sub, string msg1, string msg2)
{
    MailMessage mailToAdmin = new MailMessage();
    mailToAdmin.From = new MailAddress("Portal 123<123@123.info>");
    mailToAdmin.To.Add(new MailAddress("123@123.info"));
    mailToAdmin.Subject =sub;
    mailToAdmin.Body = msg1;
    mailToAdmin.IsBodyHtml = true;

    try
    {
        SmtpClient smtp = new SmtpClient();
        smtp.Send(mailToUser);
        smtp.Send(mailToAdmin);
    }
    catch (Exception e)
    {
        return e.Message;
    }
}

Open in new window

0
 

Author Comment

by:calypsoworld
ID: 33506792
After the modification, same error.
0
 
LVL 8

Expert Comment

by:RyanAndres
ID: 33506809
Well I can't identify the error. Try debugging and check that the smtpclient object has all necessary values set before the mail is sent.
0
 

Author Comment

by:calypsoworld
ID: 33620526
I didn't find anything wrong during debug, but I have to test again and check a couple things...
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

717 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