Click here to Skip to main content
Sign Up to vote bad
good
Hi,
 
I am using the following code to send mail with attachment . The function below works fine sends mail but does not attach the attachments to it Please help me in this :
 
 Dim msg As New System.Web.Mail.MailMessage
            If BodyFormat = "HTML" Then
                msg.BodyFormat = MailFormat.Html
            Else
                msg.BodyFormat = MailFormat.Text
            End If
            If Not String.IsNullOrEmpty(Attachment) Then
                msg.Attachments.Add((New MailAttachment(Attachment)))
            End If
            msg.From = FromEmail
            msg.Body = Body
            msg.Subject = Subject
            msg.To = ToEmail
 
            SmtpMail.SmtpServer = ConfigurationSettings.AppSettings("SMTPSvr")
            Try
                'SmtpMail.Send(FromEmail, ToEmail, Subject, Body)
                SmtpMail.Send(msg)
 
                ReturnMsg = "Success"
            Catch ex As Exception
                ReturnMsg = "An error has occurred while sending Email. Please try again. [" + ex.Message + "]"
                ''While Not ex.InnerException Is Nothing
                ''    ReturnMsg += vbCrLf
                ''    ReturnMsg += ex.InnerException.ToString
                ''    ex = ex.InnerException
                ''End While
            End Try
Posted 26 Feb '13 - 23:26
Edited 26 Feb '13 - 23:29


5 solutions

Email Attachment Code in C#
 
public bool sendemail(string to, string replyto, string body, string subject, FileUpload f1, FileUpload f2)
    {
        MailMessage mail = new MailMessage();
        mail.To.Add(to);
        mail.From = new MailAddress("xyz@gmail.com","Subject of Email");
        mail.Subject = subject;
        mail.Body = body;
        MailAddress rt = new MailAddress(replyto);
        mail.ReplyTo = rt;
        //mail.Sender = rt;
        mail.IsBodyHtml = true;
        if (f1.HasFile)
        {
            mail.Attachments.Add(new Attachment(f1.PostedFile.InputStream, f1.FileName)); //add the attachment
        }
        if (f2.HasFile)
        {
            mail.Attachments.Add(new Attachment(f2.PostedFile.InputStream, f2.FileName)); //add the attachment
        }
 
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Credentials = new System.Net.NetworkCredential("xyz@gmail.com", "password");
 
        smtp.Port = 587;
        smtp.EnableSsl = true;
        try
        {
            smtp.Send(mail);
            return (true);
        }
        catch (Exception ex)
        {
            return (false);
        }
    }
 
Thanks
Source : Email Sending with attachment asp.net c#[^]
  Permalink  
  Permalink  
Design Page:
 
<table>
        <tr>
            <td>Message to: </td>
            <td><asp:TextBox ID="txtTo" runat="server" /></td>
        </tr>
        <tr>
            <td> Message from: </td>
            <td><asp:TextBox ID="txtFrom" runat="server" /></td>
        </tr>
        <tr>
            <td>Subject: </td>
            <td><asp:TextBox ID="txtSubject" runat="server" />
            </td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td>
                <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Attachment</asp:LinkButton>
                <asp:FileUpload ID="FileUpload1" runat="server" />
            </td>
        </tr>
        <tr>
            <td> Message Body:</td>
            <td><asp:TextBox ID="txtBody" runat="server" Height="171px" TextMode="MultiLine" Width="270px" /></td>
        </tr>
        <tr>
            <td></td>
            <td><asp:Button ID="Btn_SendMail" runat="server" onclick="Btn_SendMail_Click" Text="Send Email" /></td>
        </tr>
        <tr>
            <td></td>
            <td><asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></td>
        </tr>
    </table>
---------------------------------------------------------------------------------
 
C# Codings:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Windows.Forms;
 
public partial class CAS_SendingMail1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        FileUpload1.Visible = false;
    }
    protected void Btn_SendMail_Click(object sender, EventArgs e)
    {
        MailMessage mail = new MailMessage();
        mail.To.Add(txtTo.Text);
        mail.From = new MailAddress(txtFrom.Text);
        mail.Subject = txtSubject.Text;
        mail.Body = txtBody.Text;
        mail.IsBodyHtml = true;
 
        if (FileUpload1.HasFile)
        {
 
        mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream,  FileUpload1.FileName));
        }
        SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
        smtp.EnableSsl = true;
        smtp.UseDefaultCredentials = false;
        smtp.Credentials = new System.Net.NetworkCredential("fast2mailme@gmail.com", "naren143dotnet");
        smtp.Send(mail);
 
    }
 
    protected void LinkButton1_Click1(object sender, EventArgs e)
    {
        FileUpload1.Visible = true;
    }
}
  Permalink  
Hi,
 
I am using System.Web.Mail.MailMessage object and not System.Net.Mail object. Also in the code msg.Attachments.Add((New MailAttachment(Attachment))) I am passing Attachment as a path from physical location e.g :-
 
msg.Attachments.Add((New MailAttachment("C:\\Temp\Test.ics")))
 

The problem I am facing is just mail is being sent and not the attachment.
Please help me in this.
 
Thanks,
  Permalink  
Comments
aspnet_regiis -i - 27 Feb '13 - 12:36
try sending some other file other than .ics
rahulkasar - 28 Feb '13 - 4:05
Hi, I am using System.Web.Mail.MailMessage object and not System.Net.Mail object. Also in the code msg.Attachments.Add((New MailAttachment(Attachment))) I am passing Attachment as a path from physical location e.g :- msg.Attachments.Add((New MailAttachment("C:\\Temp\Test.ics"))) The problem I am facing is just mail is being sent and not the attachment. Please help me in this. Thanks,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 353
1 OriginalGriff 345
2 Arun Vasu 315
3 Maciej Los 208
4 Aarti Meswania 180
0 Sergey Alexandrovich Kryukov 9,680
1 OriginalGriff 7,539
2 CPallini 4,018
3 Rohan Leuva 3,362
4 Maciej Los 2,951


Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 27 Feb 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid