ASP.NET - Send emil with multiple attachmenet files

Asked By mostafa hamdy on 14-Mar-11 09:32 AM
Hello all
I want to create some web form in asp.net C3 which will send email and this email may have more than one attachment file ,,please if any bidy get what I mean and can help me please send me or tell me about some url may help me in doing that
regards'
Mostafa
Kirtan Patel replied to mostafa hamdy on 14-Mar-11 09:36 AM
Here is code to send Mail with Multiple Files Attached ( Used Array List for that purpose here )

//local machine is used
//AttachmentFile (first function: Optional, file name)
//AttachmentFiles (second function: Optional, list of  
//attachments in form of an array list)
 
 
public void SendMailOneAttachment(string From, string sendTo, string Subject, string Body, string AttachmentFile = "", string CC = "", string BCC = "", string SMTPServer = "")
{
  MailMessage myMessage = default(MailMessage);
 
  try {
    myMessage = new MailMessage();
    var _with1 = myMessage;
    _with1.To = sendTo;
    _with1.From = From;
    _with1.Subject = Subject;
    _with1.Body = Body;
    _with1.BodyFormat = MailFormat.Text;
    //CAN USER MAILFORMAT.HTML if you prefer
 
    if (!string.IsNullOrEmpty(CC))
      _with1.Cc = CC;
    if (!string.IsNullOrEmpty(BCC))
      _with1.Bcc = "";
 
    if (FileExists(AttachmentFile))
      _with1.Attachments.Add(AttachmentFile);
 
 
    if (!string.IsNullOrEmpty(SMTPServer))
      SmtpMail.SmtpServer = SMTPServer;
    SmtpMail.Send(myMessage);
 
  } catch (Exception myexp) {
    throw myexp;
  }
 
}
 
 
public void SendMailMultipleAttachments(string From, string sendTo, string Subject, string Body, ArrayList AttachmentFiles = null, string CC = "", string BCC = "", string SMTPServer = "")
{
  MailMessage myMessage = default(MailMessage);
  int i = 0;
  int iCnt = 0;
 
  try {
    myMessage = new MailMessage();
    var _with2 = myMessage;
    _with2.To = sendTo;
    _with2.From = From;
    _with2.Subject = Subject;
    _with2.Body = Body;
    _with2.BodyFormat = MailFormat.Text;
    //CAN USER MAILFORMAT.HTML if you prefer
 
    if (!string.IsNullOrEmpty(CC))
      _with2.Cc = CC;
    if (!string.IsNullOrEmpty(BCC))
      _with2.Bcc = "";
 
    if ((AttachmentFiles != null)) {
      iCnt = AttachmentFiles.Count - 1;
      for (i = 0; i <= iCnt; i++) {
        if (FileExists(AttachmentFiles[i]))
          _with2.Attachments.Add(AttachmentFiles[i]);
      }
 
    }
 
 
    if (!string.IsNullOrEmpty(SMTPServer))
      SmtpMail.SmtpServer = SMTPServer;
    SmtpMail.Send(myMessage);
 
 
  } catch (Exception myexp) {
    throw myexp;
  }
}
 
private bool FileExists(string FileFullPath)
{
  if (string.IsNullOrEmpty(Strings.Trim(FileFullPath)))
    return false;
 
  System.IO.FileInfo f = new System.IO.FileInfo(FileFullPath);
  return f.Exists;
 
}
Reena Jain replied to mostafa hamdy on 14-Mar-11 09:44 AM
hi,

here is the code
MailMessage mail = new MailMessage();
     SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
  
     //get the userID, Pass
     userID= register.userName;
     password = register.pass;
     string aa=txtTo.Text;
     mail.From = new MailAddress(userID);
     mail.To.Add(aa);
     mail.Subject = txtsubject.Text;
     mail.Body = txtComments.Text;
  
     //Attach file
     mail.Attachments.Add(new Attachment(txtAttachments4.Text.ToString()));
     mail.Attachments.Add(new Attachment(txtAttachments1.Text.ToString()));
     mail.Attachments.Add(new Attachment(txtAttachments2.Text.ToString()));
    mail.Attachments.Add(new Attachment(txtAttachments3.Text.ToString()));
     SmtpServer.Port = 587;
     SmtpServer.UseDefaultCredentials = false;
     SmtpServer.Credentials = new System.Net.NetworkCredential(userID, password);
     SmtpServer.EnableSsl = true;
     SmtpServer.Send(mail);
     MessageBox.Show("Email sent successfully");
     this.Cursor = Cursors.Default;
  
     //close the page
     Email email = new Email();
     email.Close();

Hope this will help you
Ravinder Jamgotre replied to mostafa hamdy on 14-Mar-11 09:59 AM

Hi Mostafa,

The issue with this part is:

the user attaches less than five, the email will contain 5 attachments. Also, there is nodialog box to warn the user if there is no attachments.

See at the end of this code sample and ELSE statement that will eliminate the issues; and give you a dream code:


<%@ Page Language="VB" %> 
 
<%@ Import Namespace="System.Net.Mail" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<script runat="server"> 
   
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) 
        
   
End Sub 
    
   
Protected Sub
SendEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs) 
 
       
Dim mail As New MailMessage() 
        mail
.From = New MailAddress("Ben@dharma.org") 
        mail
.To.Add("RogerWorkman@dharma.org") 
         
        mail
.Subject = "Attachment Test" 
        mail
.Body = "Check out the attachment!" 
        
        
         
     
If FileUpload1.FileName is dbnull.value Then 
           
SendEmail.Attributes.Add("onclick", "return confirm('No attachment is selected, is this OK?')") 
                     
       
Else 
            mail
.Attachments.Add(New Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName)) 
            mail
.Attachments.Add(New Attachment(FileUpload2.PostedFile.InputStream, FileUpload2.FileName)) 
            mail
.Attachments.Add(New Attachment(FileUpload3.PostedFile.InputStream, FileUpload3.FileName)) 
            mail
.Attachments.Add(New Attachment(FileUpload4.PostedFile.InputStream, FileUpload4.FileName)) 
            mail
.Attachments.Add(New Attachment(FileUpload5.PostedFile.InputStream, FileUpload5.FileName)) 
 
           
'send the message 
            Dim smtp As New SmtpClient("
smtp_server_name") 
            smtp
.Send(mail) 
         
       
End If 
        
   
End Sub 
 
</script> 
 
<html xmlns="
http://www.w3.org/1999/xhtml"> 
<head runat="
server"> 
   
<title>Untitled Page</title> 
     
</head> 
<body> 
 
   
<form runat="
server" id="form1"> 
       
<table border="
0"> 
           
<tr> 
               
<td> 
                   
</td> 
               
<td> 
                   
</td> 
           
</tr> 
           
<tr> 
               
<td> 
                   
<b>File to Send:</b></td> 
               
<td> 
                   
<asp:FileUpload ID="
FileUpload1" runat="server" /> 
                   
<asp:FileUpload ID="
FileUpload2" runat="server" /> 
                   
<asp:FileUpload ID="
FileUpload3" runat="server" /> 
                   
<asp:FileUpload ID="
FileUpload4" runat="server" /> 
                   
<asp:FileUpload ID="
FileUpload5" runat="server" /> 
               
</td> 
           
</tr> 
           
<tr> 
               
<td colspan="
2"> 
                   
<b>Body:</b><br /> 
                   
<asp:TextBox runat="
server" ID="Body" TextMode="MultiLine" Columns="55" Rows="10"></asp:TextBox> 
               
</td> 
           
</tr> 
           
<tr> 
               
<td colspan="
2" align="center"> 
                   
<asp:Label ID="
lblmessage" runat="server"></asp:Label> 
                   
<br /> 
                   
<asp:Button runat="
server" ID="SendEmail" Text="Send" OnClick="SendEmail_Click" /> 
               
</td> 
           
</tr> 
       
</table> 
   
</form> 
</body> 
</html>




 Else

  if FileUpload1.FileName.trim <> "" then
       mail.Attachments.Add(New Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName))
  endif
  if FileUpload2.FileName.trim <> "" then
       mail.Attachments.Add(New Attachment(FileUpload2.PostedFile.InputStream, FileUpload2.FileName))
  endif
  if FileUpload3.FileName.trim <> "" then
       mail.Attachments.Add(New Attachment(FileUpload3.PostedFile.InputStream, FileUpload3.FileName))
  endif
  if FileUpload4.FileName.trim <> "" then
       mail.Attachments.Add(New Attachment(FileUpload4.PostedFile.InputStream, FileUpload4.FileName))
  endif
  if FileUpload5.FileName.trim <> "" then
       mail.Attachments.Add(New Attachment(FileUpload5.PostedFile.InputStream, FileUpload5.FileName))
  endif

Hope That Helps You!
Thanks
Ravinder