There are two approach to achieve it.
- Only Code behind
- Config + Code behind
var fromAddress = new MailAddress("xxxx@hotmail.com", "From Name");
var toAddress = new MailAddress("xxxx@gmail.com", "To Name");
const string fromPassword = "xxxxxxx";
const string subject = "Subject";
const string body = "Body";
var smtp = new SmtpClient
{
Host = "smtp.live.com",
Port = 25,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
}
)
{
smtp.Send(message);
}
<system.net>
<mailSettings>
<smtp from="xxx@hotmail.com" deliveryMethod="Network">
<network host="smtp.live.com" port="25"
userName="xxx@hotmail.com" password="xxxxx" enableSsl="true"/>
</smtp>
</mailSettings>
</system.net>
var message = new MailMessage();
//here is an important part:
message.To.Add(new MailAddress("xxxx@gmail.com", "Mailer"));
message.Subject = "Yelp!";
message.Body = "Yes";
// 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);