Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :

VB
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
Updated 26-Feb-13 23:29pm
v2

Email Attachment Code in C#

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#[^]
 
Share this answer
 
v2
Refer this article on CP
Send mail using System.Web.Mail namespace[^]
 
Share this answer
 
XML
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:
C#
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;
    }
}
 
Share this answer
 
v2
Use this Code for senting Email through Web mail

Step -1:

Add this following Code in your C# file.

C#
{

            string mailUsername = ConfigurationSettings.AppSettings["MailUserName"].Trim();
            string mailPassword = ConfigurationSettings.AppSettings["MailPassword"].Trim();
            string mailServer = ConfigurationSettings.AppSettings["MailServer"].Trim();

            MailMessage msg = new MailMessage();
            msg.BodyFormat = MailFormat.Html;
            msg.To = txtemail.Text.Trim();
            msg.From = mailUsername;
            msg.Subject = "Subject";

            msg.Body = string.Format("<html><head></head><body><b>Add your HTML Message</b><br/><br/>");
           
            msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
            msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", mailUsername);
            msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", mailPassword);

            SmtpMail.SmtpServer = mailServer;
            SmtpMail.Send(msg);
        }




Step -2:

Add following Code into your Web.config file


XML
<appSettings>

    <add key="MailServer" value="Your Webmail"/>
    <add key="MailUserName" value="Sender MailId " />
    <add key="MailPassword" value="Password" />
  </appSettings>
 
Share this answer
 
v4
Comments
Nelek 7-Sep-13 8:12am    
Did you realize that the question is more than 6 months old?
http://www.c-sharpcorner.com/uploadfile/9f0ae2/send-emails-with-attachment-in-asp-net/

Visit this link....
 
Share this answer
 
v2
sending mail
C#
private void btnsendmail_Click(System.Object sender, System.EventArgs e)
{
	MailMessage mail = new MailMessage();
	SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
	mail.From = new MailAddress(txtid.Text);
	mail.To.Add("Email Id To Send @rediffmail.com");
	mail.Subject = txtsub.Text;
	mail.Body = txtmess.Text;
	mail.Attachments.Add(new Attachment(OpenFileDialog1.FileName));
	SmtpServer.Port = 587;
	SmtpServer.Credentials = new System.Net.NetworkCredential(txtid.Text, txtpass.Text);
	SmtpServer.EnableSsl = true;
	SmtpServer.Send(mail);
	mail.From = new MailAddress(txtid.Text);
	Interaction.MsgBox("E-mail Has Been Send Successfully !");

}

Attachment send
C#
private void btnattachement_Click(System.Object sender, System.EventArgs e)
{
    OpenFileDialog1.ShowDialog();

}
 
Share this answer
 
v2
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,
 
Share this answer
 
Comments
bbirajdar 27-Feb-13 12:36pm    
try sending some other file other than .ics
rahulkasar 28-Feb-13 4:05am    
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900