Wednesday, December 3, 2008

Sending Mail Using SMTP Server Credentials

Hi..

Here you will find the quick reference of mail Sending.
have a look and enjoy..
==================================================================
The Class File Where The Email Manager class resides...
==================================================================
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Configuration;
namespace CommonEmailManager
{
public class MailManager
{
private bool _issent;
private string _toAddress;
private string _strFileName;
public MailManager()
{
}
///
/// This Method Is used to send the mail
///

/// Contents of an e-mail
public void Send(MailContents emailcontents)
{
SmtpClient client = new SmtpClient("smtp.gmail.com", 587); //add port number 587
NetworkCredential mailAuth = new NetworkCredential("MyMaiId@gmail.com", "MyPwd");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = mailAuth;
MailAddress from = new MailAddress(emailcontents.FromEmailAddress, emailcontents.FromName);
MailAddress to = new MailAddress(ToAddress);
MailMessage message = new MailMessage(from, to);
message.Subject = emailcontents.Subject;
message.Body = emailcontents.Body;
message.IsBodyHtml = true;
if (emailcontents.FileName != null)
{
message.Attachments.Add(new Attachment(emailcontents.FileName));
}
try
{
client.Send(message);
IsSent = true;
}
catch (Exception ex)
{
throw ex;
}

}

///
/// This Method Is used to send the mail having with an attachment with it
///

/// Contents of an e-mail
public void SendAttachment(MailContents emailcontents)
{
SmtpClient client = new SmtpClient("smtp.gmail.com", 587); //add port number 587
NetworkCredential mailAuth = new NetworkCredential("MyMaiId@gmail.com", "MyPwd");
client.EnableSsl = true;client.UseDefaultCredentials = false;
client.Credentials = mailAuth;MailAddress from = new MailAddress(emailcontents.FromEmailAddress, emailcontents.FromName);
MailAddress to = new MailAddress(ToAddress);
MailMessage message = new MailMessage(from, to);
message.Subject = emailcontents.Subject;
message.Body = emailcontents.Body;
message.IsBodyHtml = true;
Attachment atch = new Attachment(emailcontents.FileName);
message.Attachments.Add(atch);
try
{
client.Send(message);
IsSent = true;
}
catch (Exception ex)
{
throw ex;
}
finally
{
atch.Dispose();

}

}

public bool IsSent
{
get { return _issent; }
set { _issent = value; }
}
private string SMTPServerName
{
get { return ConfigurationManager.AppSettings["SMTPServer"]; }
}
public string ToAddress
{

get
{
return _toAddress;
}
set
{
_toAddress = value;
}

}
}
public struct MailContents
{
public string To;
public string FromName;
public string FromEmailAddress;
public string Subject;
public string Body;
public string FileName;

}

}


==================================================================

The Page code from where We send the Email ...And on Which WE call the emailManger.cs class methods.
==================================================================

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Web.Mail;
using System.IO;
using CommonEmailManager;

public partial class Admin_AdminNewLetterSend : System.Web.UI.Page
{
//Do Your Code
protected void Page_Load(object sender, EventArgs e)
{
//Do Your Code
}

protected void btnSendmail_Click(object sender, EventArgs e)
{
string strMsg = string.Empty;
try
{
//targetEmailId will be get in any how...
SendingMail(targetEmailId);
strMsg = "The News Letter has been sent successfully...";
}
catch (Exception ex)
{
strMsg = "There is Some Error to send the newsletter this time .Please try again";
}
finally
{
ConfirmJScript(strMsg);
}
}
public void ConfirmJScript(string strMsg)
{
string JScript = "alert('" + strMsg + "');";
JScript = "";
ClientScript.RegisterStartupScript(this.GetType(), "JScript", JScript);
}
public void SendingMail(string TargetEmailId)
{
CommonEmailManager.MailManager emailmngr = new MailManager();
CommonEmailManager.MailContents contents = new MailContents();
string msgBody = "my Message Body(I May be HTML Body)";
contents.Body = msgBody;
contents.FromEmailAddress = "My EmailID@gmail.com";
contents.Subject = "My Meassage";
emailmngr.ToAddress = TargetEmailId;
emailmngr.Send(contents);
}

}
==================================================================

Enjoy mail Sending

Sanjeev Kumar
HelpOnDesk Team

3 comments:

Anonymous said...

Who knows where to download XRumer 5.0 Palladium?
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.