• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3653
  • Last Modified:

how send email through smtp in asp.net


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
arhc
Asked:
arhc
  • 7
  • 4
  • 4
  • +1
1 Solution
 
DhaestCommented:
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
 
HugoHiaslCommented:
Are you sure that the port 2525 ist correct?

The standard port is 25 !
0
 
John ClaesSenior .Net Consultant & Technical AnalistCommented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
arhcAuthor Commented:
@ 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
 
DhaestCommented:
>> i amgetting error at smtp.Send(msg);

I know that, but what error do you get ?
0
 
arhcAuthor Commented:
ia m getting excetion at smtp.servicepoint .Address as read below
Address = 'smtp.ServicePoint.Address' threw an exception of type 'System.NotSupportedException'
0
 
DhaestCommented:
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
 
DhaestCommented:
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
 
arhcAuthor Commented:
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
 
arhcAuthor Commented:
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
 
John ClaesSenior .Net Consultant & Technical AnalistCommented:
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
 
John ClaesSenior .Net Consultant & Technical AnalistCommented:
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
 
John ClaesSenior .Net Consultant & Technical AnalistCommented:
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
 
arhcAuthor Commented:
@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
 
arhcAuthor Commented:
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
 
arhcAuthor Commented:
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

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 7
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now