Solved

SMTP e MailSetting in web.config

Posted on 2010-08-23
13
1,324 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

832 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