Sending Mail with attachment, Inline Image using .net
This is a simple web application for sending external mail to multiple (or single) recipient, Message with an attachment or as inline image using ASP.NET 2.0 and C# and VB
Features
1.Send mail to multiple receipients(or single)
2.Send mail with attachment
3.Send Mail with inline image(Add imgae direclty to message space)
4.Send as CC and BCC
VB
Import the following namespaces
Imports System.Net.Mail
Imports System.Net.Mail.SmtpClient
Imports System.Net.Mail.MailMessage
Decalration part
Private mMailServer As String
Private mTo As String
Private mFrom As String
Private mMsg As String
Private mSubject As String
Private mPort As Integer
Public flag As Integer = 0
Dim a_file As String
Protected Sub btnsend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnsend.Click
Try
'set the addresses
mFrom = txtfrom.Text.Trim()
mTo = Trim(txtto.Text)
'set the content
mSubject = txtsubject.Text.Trim()
mMsg = txtmessage.Text
mMailServer = "localhost"
mPort = 25
Dim message As New System.Net.Mail.MailMessage(mFrom, mTo, mSubject, mMsg)
'*************************Send Attchment *************************'
If FileUpload1.FileName <> "" Then
message.Attachments.Add(New Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName))
Else
lblmessage.Text = "Select Attachemnt!!!"
Exit Sub
End If
'--------------------------------------End----------------------------'
'*************************Send Inline Image ***********************'
If FileUpload1.FileName <> "" Then
'create the Plain Text part
Dim plainView As AlternateView = AlternateView.CreateAlternateViewFromString("", Nothing, "text/plain")
' create the Html part
'to embed images, we need to use the prefix 'cid' in the img src value
'the cid value will map to the Content-Id of a Linked resource.
'thus <img src='cid:image'> will map to a LinkedResource with a ContentId of 'image'
Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString(".<img src=cid:image>", Nothing, "text/html")
'select the image
Dim at As New Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName)
'create LinkedResource (embedded image)
Dim logo As New LinkedResource(FileUpload1.PostedFile.InputStream, "text/plain")
logo.ContentId = "image"
'add the LinkedResource to the appropriate view
htmlView.LinkedResources.Add(logo)
'add the views
message.AlternateViews.Add(plainView)
message.AlternateViews.Add(htmlView)
End if
'--------------------------------------End----------------------------'
'******************************Send CC********************************'
If txtbcc.Text <> "" Then
message.Bcc.Add(txtbcc.Text.Trim())
End If
'-------------------------------End-----------------------------------'
'******************************Send BCC*******************************'
If txtcc.Text <> "" Then
message.CC.Add(txtcc.Text.Trim())
End If
'-------------------------------End----------------------------------'
'send the message
Dim mySmtpClient As New SmtpClient(mMailServer, mPort)
mySmtpClient.UseDefaultCredentials = True
mySmtpClient.Host = "<”your ip address here”>"
mySmtpClient.Send(message)
clear()
lblmessage.Text = "Mail has been sent Successfully !!!"
Catch ex As Exception
lblmessage.Text = ex.Message
End Try
End Sub
C#
Import the follwing namespace
Using System.Net.Mail
Using System.Net.Mail.SmtpClient
Using System.Net.Mail.MailMessage
Decalration
private string mMailServer;
private string mTo;
private string mFrom;
private string mMsg;
private string mSubject;
private int mPort;
public int flag = 0;
string a_file;
protected void btnsend_Click(object sender, System.EventArgs e)
{
try {
//set the addresses
mFrom = txtfrom.Text.Trim();
mTo = Strings.Trim(txtto.Text);
//set the content
mSubject = txtsubject.Text.Trim();
mMsg = txtmessage.Text;
mMailServer = "localhost";
mPort = 25;
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(mFrom, mTo, mSubject, mMsg);
//*************************Send Attachment *************************'
if (!string.IsNullOrEmpty(FileUpload1.FileName)) {
message.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
}
else {
lblmessage.Text = "Select Attachemnt!!!";
return;
}
//--------------------------------------End----------------------------'
//*************************Send Inline Image ***********************'
if (!string.IsNullOrEmpty(FileUpload1.FileName)) {
//create the Plain Text part
AlternateView plainView = AlternateView.CreateAlternateViewFromString("", null, "text/plain");
// create the Html part
//to embed images, we need to use the prefix 'cid' in the img src value
//the cid value will map to the Content-Id of a Linked resource.
//thus <img src='cid:image'> will map to a LinkedResource with a ContentId of 'image'
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(".<img src=cid:image>", null, "text/html");
//select the image
Attachment at = new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName);
//create LinkedResource (embedded image)
LinkedResource logo = new LinkedResource(FileUpload1.PostedFile.InputStream, "text/plain");
logo.ContentId = "image";
//add the LinkedResource to the appropriate view
htmlView.LinkedResources.Add(logo);
//add the views
message.AlternateViews.Add(plainView);
message.AlternateViews.Add(htmlView);
}
//--------------------------------------End----------------------------'
//******************************Send CC******************************'
if (!string.IsNullOrEmpty(txtbcc.Text)) {
message.Bcc.Add(txtbcc.Text.Trim());
}
//-------------------------------End-----------------------------------'
//******************************Send BCC**************************'
if (!string.IsNullOrEmpty(txtcc.Text)) {
message.CC.Add(txtcc.Text.Trim());
}
//-------------------------------End----------------------------------'
//send the mail
SmtpClient mySmtpClient = new SmtpClient(mMailServer, mPort);
mySmtpClient.UseDefaultCredentials = true;
mySmtpClient.Host = "<”your ip address here”>";
mySmtpClient.Send(message);
clear();
lblmessage.Text = "Mail has been sent Successfully !!!";
}
catch (Exception ex) {
lblmessage.Text = ex.Message;
}
}
Sending Mail to Multiple Recipients
VB
Protected Sub lstto_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstto.SelectedIndexChanged
Try
If lstto.SelectedIndex > 0 Then ‘if None
If lstto.SelectedIndex = 1 Then ‘ if All
txtto.Text = ""
For Each li As ListItem In lstto.Items
If li.Text <> "All" And li.Text <> "None" Then
li.Value = li.Value
txtto.Text += li.Value + ","
End If
Next
ElseIf lstto.SelectedIndex > 1 Then
'txtto.Text = ""
For Each li As ListItem In lstto.Items
If li.Selected = True Then
li.Value = li.Value
txtto.Text += li.Value + ","
End If
Next
End If
Else
txtto.Text = ""
End If
Catch ex As Exception
lblmessage.Text = ex.Message
End Try
End Sub
C#
protected void lstto_SelectedIndexChanged(object sender, System.EventArgs e)
{
try {
//if None
if (lstto.SelectedIndex > 0) {
// if All
if (lstto.SelectedIndex == 1) {
txtto.Text = "";
foreach (ListItem li in lstto.Items) {
if (li.Text != "All" & li.Text != "None") {
li.Value = li.Value;
txtto.Text += li.Value + ",";
}
}
}
else if (lstto.SelectedIndex > 1) {
//txtto.Text = ""
foreach (ListItem li in lstto.Items) {
if (li.Selected == true) {
li.Value = li.Value;
txtto.Text += li.Value + ",";
}
}
}
}
else {
txtto.Text = "";
}
}
catch (Exception ex) {
lblmessage.Text = ex.Message;
}
}