Web.config Setup:
<system.net>
<mailSettings>
<smtp from="noreply@yourdomain.com">
<network host="smtp.gmail.com"
password="******" userName="noreply@yourdomain.com" port="587" />
</smtp>
</mailSettings>
</system.net>
C# Send
Mail Code:
try
{
MailMessage message = new MailMessage();
message.From = new MailAddress(tbxEmail.Text);
message.To.Add(new MailAddress(_supportEmail));
message.Subject = tbxSubject.Text;
message.IsBodyHtml = false;
message.Priority = MailPriority.High;
message.Body = tbxMessage.Text;
SmtpClient client = new SmtpClient();
client.EnableSsl = true;
client.Send(message);
return true;
}
catch (Exception ex)
{
ExceptionHandler handler = new ExceptionHandler();
handler.LogException(ex,
SeverityLevel.Critical,
"Contact Form Message could not be sent to support email",
GetType(),
"SendMessageEmail");
}
//---------------email code
using System.Net.Mail;
using System.Net;
...
SmtpClient mailClient = new SmtpClient(
"smtp.gmail.com", "587");
mailClient.EnableSsl = true;
NetworkCredential cred = new NetworkCredential(
"your_gmail_username",
"your_gmail_password");
mailClient.Credentials =
cred;
mailClient.Send("from_me@gmail.com", "to_you@gmail.com",
"subject goes here", "email body goes here");
//----------------------------- smtp code
//yahoo --->
smtp.mail.yahoo.com (port 25)
//gmail --->
smtp.gmail.com (SSL enabled, port 465)
//AOL ---> smtp.aol.com
//Netscape --->
smtp.isp.netscape.com (port 25)
//--------copde
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("someone@somewhere.com");
mailMessage.Subject = "Test";
mailMessage.Body = "<html><body>This is a test</body></html>";
mailMessage.IsBodyHtml = true;
// Create the credentials to login to the gmail account associated with my custom domain
string sendEmailsFrom = "emailAddress@mydomain.com";
string sendEmailsFromPassword = "password";
NetworkCredential cred = new NetworkCredential(sendEmailsFrom, sendEmailsFromPassword);
SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);
mailClient.EnableSsl = true;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.UseDefaultCredentials = false;
mailClient.Timeout = 20000;
mailClient.Credentials = cred;
mailClient.Send(mailMessage);
<configuration>
<system.net>
<mailSettings>
<smtp from="example@domain.com" deliveryMethod="Network">
<network host="smtp.gmail.com" port="587"
userName="example@domain.com" password="password"/>
</smtp>
</mailSettings>
</system.net>
</configuration>
End when
you send email just enable SSL on your SmtpClient:
var message = new MailMessage("navin@php.net");
// here is an important part:
message.From = new MailAddress("example@domain.com", "Mailer");
// it's superfluous part here since from address is defined in .config file
// in my example. But since you don't use .config file, you will need it.
var client = new SmtpClient();
client.EnableSsl = true;
client.Send(message);
//------------
Security Exception
Description: The application attempted to perform an operation not allowed by
the security policy. To grant this application the required permission
please contact your system administrator or change the application's trust
level in the configuration file.
Exception Details: System.Security.SecurityException: Request for
the permission of type 'System.Net.Mail.SmtpPermission, System,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
This
is my _AppStart.cshtml content :
@{
WebSecurity.InitializeDatabaseConnection("usersdb",
"UserProfile", "UserId", "Email", true);
WebMail.SmtpServer = "smtp.gmail.com";
WebMail.EnableSsl = true;
WebMail.SmtpPort = 587;
WebMail.UserName =
"no-reply@imperialcinemas.net";
WebMail.Password = "**********";
WebMail.From = "no-reply@imperialcinemas.net";
}
using System.Net.Mail;
private void SendMail()
{
MailMessage message = new MailMessage();
message.To.Add( "testing@anydomain.com" );
message.From = new MailAddress( "email@mydomain.com", "Name to Display"
System.Text.Encoding.UTF8 );
message.Subject = "Testing";
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.Body = "Testing 123";
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = false;
message.Priority = MailPriority.Normal;
SmtpClient emailClient = new SmtpClient();
emailClient.Credentials = new NetworkCredential( "username@mydomain.com", "yourpassword" );
emailClient.Port = 587;
emailClient.Host = "smtp.gmail.com"
emailClient.EnableSsl = true;
emailClient.Send( message );
}