Solved

how send email through smtp in asp.net

Posted on 2011-03-01
16
3,164 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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
 

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

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