|
Requirements for Sending mail via Office 365
The application must connect to the Office 365 servers, with port 587
The application must support TLS(Encryption method)
The application must authenticate with Office 365
The account you authenticate to the server with must be the same account as the from address on the mail you send through the Office365.
Code:
using System;
using System.Web.UI;
using System.Net.Mail;
protected void btnSend_Click(object sender, EventArgs e)
{
try
{
if (SendMail() == true)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('Mail send sucessfully.')", true);
}
else
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('Sending Failed.')", true);
}
}
catch (Exception ex)
{
}
}
private bool SendMail()
{
try
{
SmtpClient oSmtpClient = new SmtpClient("smtp.office365.com",587);
oSmtpClient.EnableSsl = true;
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("youroffice365mail@mail.org", "Your Password");
oSmtpClient.Credentials = cred;
MailMessage oMailMessage = new MailMessage();
oMailMessage.From = new MailAddress("youroffice365mail@mail.org","User Name");
oMailMessage.To.Add("recipientmail@mail.com");
oMailMessage.Subject = "Subject";
oMailMessage.Body = "Mail Body";
oSmtpClient.Send(oMailMessage);
return true;
}
catch (Exception ex)
{
return false;
}
}
MaheshUBhosale
|
|
|
|