C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Email »

Sending Mail to Multiple recipients with attachement or as Inline Image


Posted Date: 04 Feb 2009    Resource Type: Code Snippets    Category: Email
Author: HygeenaMember Level: Gold    
Rating: 1 out of 5Points: 15





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.Hope this snippet will help u...

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 will map to a LinkedResource with a ContentId of 'image'

Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString(".", 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







************* Using 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 will map to a LinkedResource with a ContentId of 'image'

AlternateView htmlView = AlternateView.CreateAlternateViewFromString(".", 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;
}
}


Attachments

  • Screen Shot. (25711-4244-Mail.doc)


  • Responses


    No responses found. Be the first to respond and make money from revenue sharing program.

    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sign In to add tags.
    Email  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: Sending email through C#
    Previous Resource: Email Sending
    Return to Discussion Resource Index
    Post New Resource
    Category: Email


    Post resources and earn money!
     
    More Resources



    dotNet Slackers

    About Us    Contact Us    Privacy Policy    Terms Of Use