how to configure smtp settings?

11,566

Set your credentials for your email account, let's say you are using the your Gmail account to send email, So, you have to set the credentials (username and password) to your smtpClient object.

Following is the example::

 MailMessage m = new MailMessage();
 SmtpClient sc = new SmtpClient();

MailMessage helps us to create the mail and SmtpClient allows us to send mail to the reciepient.

NOTE: the following code is configured to send mails from GMAIL account..

m.From = new MailAddress("[email protected]", "Display name");
m.To.Add(new MailAddress("[email protected]", "Display name To"));
m.Subject = "Test";
m.Body = "This is a Test Mail";
sc.Host = "smtp.gmail.com";
sc.Port = 587;
sc.Credentials = new System.Net.NetworkCredential("[email protected]", "password of from");
sc.EnableSsl = true; // runtime encrypt the SMTP communications using SSL
sc.Send(m);
Share:
11,566
Hadi Nemati
Author by

Hadi Nemati

Updated on June 04, 2022

Comments

  • Hadi Nemati
    Hadi Nemati almost 2 years

    I wanted to recover password using PasswordRecovery control in asp.net .but when I clicked on submit button .I have got this message that :

    Bad sequence of commands. The server response was: This mail server requires authentication when attempting to send to a non-local e-mail address. Please check your mail client settings or contact your administrator to verify that the domain or address is defined for this server . this is my html code:

    <asp:PasswordRecovery ID="PasswordRecovery1" runat="server" 
              style="font-family: Tahoma; font-size: small" 
              onsendingmail="PasswordRecovery1_SendingMail" >
              <MailDefinition BodyFileName="../User Pages/RecoveryPass.htm" From="[email protected]"
                IsBodyHtml="True" Subject="بازیابی کلمه ی عبور" Priority="High"></MailDefinition>
            <UserNameTemplate>
                <table cellpadding="1" cellspacing="0" style="border-collapse:collapse;">
                    <tr>
                        <td>
                            <table cellpadding="0">
                                <tr>
                                    <td align="center" colspan="2">
                                        &nbsp;</td>
                                </tr>
                                <tr>
                                    <td align="center" colspan="2">
                                        <asp:Label ID="Label1" runat="server" 
                                            Text="<%$ Resources:resource, passrecovery %>" 
                                            style="font-size: small; font-family: Tahoma"></asp:Label></td>
                                </tr>
                                <tr>
                                    <td align="right">
                                        <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName"></asp:Label>
                                    </td>
                                    <td>
                                        <asp:TextBox ID="UserName" runat="server"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" 
                                            ControlToValidate="UserName" ErrorMessage="User Name is required." 
                                            ToolTip="User Name is required." ValidationGroup="PasswordRecovery1">*</asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="center" colspan="2" style="color:Red;">
                                        <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right" colspan="2">
                                        <asp:Button ID="SubmitButton" runat="server" CommandName="Submit" Text="<%$ Resources:resource, submit %>" 
                                            ValidationGroup="PasswordRecovery1" Font-Names="tahoma" 
                                            Font-Size="X-Small" Width="80px" />
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </UserNameTemplate>
    </asp:PasswordRecovery>
    

    and this is the code behind:

    protected void PasswordRecovery1_SendingMail(object sender, MailMessageEventArgs e)
        {
            SmtpClient smtp = new SmtpClient();
    
            smtp.Send(e.Message);
        }
    

    and this is smtp settings in web.config :

     <system.net>
        <mailSettings >
          <smtp >
            <network host="mail.domainname.com" userName="[email protected]" password ="password" defaultCredentials="false"/>
          </smtp>
              </mailSettings>
      </system.net>