Solved

SMTP e MailSetting in web.config

Posted on 2010-08-23
13
1,320 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
 

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now