Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net.Mail;
- using System.Net;
- namespace ConsoleApplication2
- {
- class Program
- {
- static void Main(string[] args)
- {
- var client = new SmtpClient("smtp.gmail.com", 587)
- {
- Credentials = new NetworkCredential("myusername@gmail.com", "mypwd"),
- EnableSsl = true
- };
- client.Send("myusername@gmail.com", "myusername@gmail.com", "test", "testbody");
- Console.WriteLine("Sent");
- Console.ReadLine();
- }
- }
- }
- Dim SMTPClientObj As New Net.Mail.SmtpClient
- SMTPClientObj.UseDefaultCredentials = False
- SMTPClientObj.Credentials = New System.Net.NetworkCredential("myusername@gmail.com", "mypwd")
- SMTPClientObj.Host = "smtp.gmail.com"
- SMTPClientObj.Port = 587
- SMTPClientObj.EnableSsl = True
- SMTPClientObj.Send("myusername@gmail.com","yourusername@gmail.com","test","testbody")
- <configuration>
- <appSettings>
- <add key="EnableSSLOnMail" value="True"/>
- </appSettings>
- <!-- other settings -->
- ...
- <!-- system.net settings -->
- <system.net>
- <mailSettings>
- <smtp from="yourusername@gmail.com" deliveryMethod="Network">
- <network
- defaultCredentials="false"
- host="smtp.gmail.com"
- port="587"
- password="stR0ngPassW0rd"
- userName="yourusername@gmail.com"
- />
- <!-- When using .Net 4.0 (or later) add attribute: EnableSSL="True" and you're all set-->
- </smtp>
- </mailSettings>
- </system.net>
- </configuration>
- Partial Class RecoverPassword
- Inherits System.Web.UI.Page
- Protected Sub RecoverPwd_SendingMail(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MailMessageEventArgs) Handles RecoverPwd.SendingMail
- e.Message.Bcc.Add("webmaster@example.com")
- SSLMail.SendMail(e)
- End Sub
- End Class
- Partial Class RecoverPassword
- Inherits System.Web.UI.Page
- Protected Sub RecoverPwd_SendingMail(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MailMessageEventArgs) Handles RecoverPwd.SendingMail
- e.Message.Bcc.Add("webmaster@example.com")
- SSLMail.SendMail(e)
- End Sub
- End Class
- //Satrt Send Email Function
- public string SendMail(string toList, string from, string ccList, string subject, string body)
- {
- MailMessage message = new MailMessage();
- SmtpClient smtpClient = new SmtpClient();
- string msg = string.Empty;
- try
- {
- MailAddress fromAddress = new MailAddress(from);
- message.From = fromAddress;
- message.To.Add(toList);
- if (ccList != null && ccList != string.Empty)
- message.CC.Add(ccList);
- message.Subject = subject;
- message.IsBodyHtml = true;
- message.Body = body;
- smtpClient.Host = "smtp.gmail.com"; // We use gmail as our smtp client
- smtpClient.Port = 587;
- smtpClient.EnableSsl = true;
- smtpClient.UseDefaultCredentials = true;
- smtpClient.Credentials = new System.Net.NetworkCredential("Your Gmail User Name", "Your Gmail Password");
- smtpClient.Send(message);
- msg = "Successful<BR>";
- }
- catch (Exception ex)
- {
- msg = ex.Message;
- }
- return msg;
- }
- //End Send Email Function
- //Satrt Send Email Function
- public string SendMail(string toList, string from, string ccList, string subject, string body)
- {
- MailMessage message = new MailMessage();
- SmtpClient smtpClient = new SmtpClient();
- string msg = string.Empty;
- try
- {
- MailAddress fromAddress = new MailAddress(from);
- message.From = fromAddress;
- message.To.Add(toList);
- if (ccList != null && ccList != string.Empty)
- message.CC.Add(ccList);
- message.Subject = subject;
- message.IsBodyHtml = true;
- message.Body = body;
- smtpClient.Host = "smtp.gmail.com"; // We use gmail as our smtp client
- smtpClient.Port = 587;
- smtpClient.EnableSsl = true;
- smtpClient.UseDefaultCredentials = true;
- smtpClient.Credentials = new System.Net.NetworkCredential("Your Gmail User Name", "Your Gmail Password");
- smtpClient.Send(message);
- msg = "Successful<BR>";
- }
- catch (Exception ex)
- {
- msg = ex.Message;
- }
- return msg;
- }
- //End Send Email Function
- Response.Write(SendMail(recipient Address, "UserName@gmail.com", "ccList if any", "subject", "body"))
- <system.net>
- <mailSettings>
- <smtp from="myusername@gmail.com" deliveryMethod="Network">
- <network defaultCredentials="false" enableSsl="true" host="smtp.gmail.com" port="587" password="password" userName="myusername@gmail.com"/>
- </smtp>
- </mailSettings>
- </system.net>
Add Comment
Please, Sign In to add comment