Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SMTP e MailSetting in web.config

Posted on 2010-08-23
13
Medium Priority
?
1,357 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

618 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