Solved

how send email through smtp in asp.net

Posted on 2011-03-01
16
3,076 Views
Last Modified: 2012-05-11

Settings in web.config

<system.net>
            <mailSettings>
                  <smtp>
                        <network
                               host="smtp.myserver.com"
                               port="2525"
                               userName="username"
                               password="password" />
                  </smtp>
            </mailSettings>
      </system.net>
----------------------------------------------------------------------------------------------------------------
//function for sendmessage written in Global Resource.cs file for better clarity
uisng system.Net.mail; (namesapce for smtp client )
 public static void SendMessage(string email, string UserName, string Password)
        {

            MailMessage msg = new MailMessage();
            msg.Subject = "Successful Registration in Chain Marketting";
            msg.Body = "Your user name is " + UserName + "  and Password is " + Password;
             SmtpClient smtp = new SmtpClient();
            smtp.EnableSsl = true;
            smtp.Send(msg);
                   
        }


Using the function on the submit button after successfully savin in database

 GlobalResource.SendMessage("ToEmail@gmail.com", "abc@yahoo.com", "123");



I am getting sending error
0
Comment
Question by:arhc
  • 7
  • 4
  • 4
  • +1
16 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 35005162
What error do you get ?
public static void SendMessage(string email, string UserName, string Password)
        {
try
{
            MailMessage msg = new MailMessage();
            msg.Subject = "Successful Registration in Chain Marketting";
            msg.Body = "Your user name is " + UserName + "  and Password is " + Password;
             SmtpClient smtp = new SmtpClient();
            smtp.EnableSsl = true;
            smtp.Send(msg);
 }
catch (Exception ex)
{
   throw ex;
}                   
        }

Open in new window

0
 
LVL 12

Expert Comment

by:HugoHiasl
ID: 35005256
Are you sure that the port 2525 ist correct?

The standard port is 25 !
0
 
LVL 10

Expert Comment

by:John Claes
ID: 35005452
I would add somewhere the lines because you're making a Message but not declaring whereto it has to be send. .....

msg.From=new MailAddress("yourMailAdress@YourDomain");
msg.To.Add=new MailAddress("email");

regards
poor beggar
0
 

Author Comment

by:arhc
ID: 35005503
@ Dhaest: i amgetting error at smtp.Send(msg);
@HugoHiasl: : yres i m sure the port is 2525 since this is the port mentioned at the control panel of my hostserver.
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 35005507
>> i amgetting error at smtp.Send(msg);

I know that, but what error do you get ?
0
 

Author Comment

by:arhc
ID: 35005553
ia m getting excetion at smtp.servicepoint .Address as read below
Address = 'smtp.ServicePoint.Address' threw an exception of type 'System.NotSupportedException'
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 35005580
I see now, you declare an smtp-client, but you don't fill the addres

Try the following (afterwards, you'll need to retrieve the information from your xml)

SmtpClient smtp = new SmtpClient();

public static void SendMessage(string email, string UserName, string Password)
        {

            MailMessage msg = new MailMessage();
            msg.Subject = "Successful Registration in Chain Marketting";
            msg.Body = "Your user name is " + UserName + "  and Password is " + Password;
             SmtpClient smtp = new SmtpClient("smtp.myserver.com",25);
            smtp.EnableSsl = true;
            smtp.Send(msg);
                    
        }

Open in new window

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 35005592
Also, like poor_beggar mentioned, you need to add an email-adres to who you want to send it to...

See also http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.servicepoint.aspx as an example
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:arhc
ID: 35005673
i tried that before too but it still gives error

at servicepoint.Address :
base {System.SystemException} = {"This property is not supported for protocols that do not use URI."}
0
 

Author Comment

by:arhc
ID: 35005700
i had made changes after i uploaded the question
MailMessage msg = new MailMessage();
            MailAddress fromAddress = new MailAddress("from@mybox.me");
            MailAddress toAddress = new MailAddress("toemail@gmail.com");

            msg.From = fromAddress;
            msg.To.Add(toAddress);  
0
 
LVL 10

Expert Comment

by:John Claes
ID: 35005725
I would add somewhere the lines because you're making a Message but not declaring whereto it has to be send. .....

msg.From=new MailAddress("yourMailAdress@YourDomain");
msg.To.Add=new MailAddress("email");

regards
poor beggar
0
 
LVL 10

Expert Comment

by:John Claes
ID: 35005957
sorry copy paste issue !

I wanted to ask the folowing :
It looks like your using a Property that is not allowed to be used On an existing SMTP Connection :
En that is the SSL property.


If you choose to create and set all the Properties of the smtp connetion in the config file.
You should add the ssl Enabling also there.
The sslEnabling is checked BEFORE creating the smtpClient.
but when you want to setthe ssl connection after creation this is not allowed.

regards
poor beggar
0
 
LVL 10

Accepted Solution

by:
John Claes earned 500 total points
ID: 35005966
Source:http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.enablessl.aspx


Remarks

The EnableSsl property specifies whether SSL is used to access the specified SMTP mail server.

The default value for this property can also be set in a machine or application configuration file. Any changes made to the EnableSsl property override the configuration file settings.

The SmtpClient class only supports the SMTP Service Extension for Secure SMTP over Transport Layer Security as defined in RFC 3207. In this mode, the SMTP session begins on an unencrypted channel, then a STARTTLS command is issued by the client to the server to switch to secure communication using SSL. See RFC 3207 published by the Internet Engineering Task Force (IETF) for more information.

An alternate connection method is where an SSL session is established up front before any protocol commands are sent. This connection method is sometimes called SMTP/SSL, SMTP over SSL, or SMTPS and by default uses port 465. This alternate connection method using SSL is not currently supported.

You can use ClientCertificates to specify which client certificates should be used to establish the SSL connection. The ServerCertificateValidationCallback allows you to reject the certificate provided by the SMTP server. The SecurityProtocol property allows you to specify the version of the SSL protocol to use.


Note
If the EnableSsl property is set to true, and the SMTP mail server does not advertise STARTTLS in the response to the EHLO command, then a call to the Send or SendAsync methods will throw an SmtpException.
0
 

Author Comment

by:arhc
ID: 35008993
@poorbeggar
I cheked the error and i am getting it when i declare teh smtp client.
SmtpClient smtp = new SmtpClient("smtp.myserver.com",2525);
when ibrowse the propeties of smtp then in the service point.Address i am geting this exception
  " base {System.SystemException} = {"This property is not supported for protocols that do not use URI."} "
i have written to the web server hosting company lets see if they have any protocols that need to be added or something else.
if u have any other ideas then most welcome
0
 

Author Comment

by:arhc
ID: 35009108
hi guys i just did some more digging and got the exact error message
" Mailbox unavailable. The server response was : relaying not allowed "
any ideas what mught be the problem
0
 

Author Comment

by:arhc
ID: 35012269
solved the problem
in the web.onfig file the username should be in the format username@domain.com I was just giving the username but should be as shown below
<system.net>
            <mailSettings>
                  <smtp>
                        <network
                               host="smtp.myserver.com"
                               port="2525"
                               userName="username@myserver.com"
                               password="password" />
                  </smtp>
            </mailSettings>
      </system.net>
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

707 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

9 Experts available now in Live!

Get 1:1 Help Now