Solved

SMTP e MailSetting in web.config

Posted on 2010-08-23
13
1,328 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

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!

Question has a verified solution.

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

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

733 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