Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET VB.NET mailing
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 27-Feb-13 0:26am
Edited 27-Feb-13 0:29am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

Use this Code for senting Email through Web mail
 
Step -1:
 
Add this following Code in your C# file.
 
{
 
            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
 

<appSettings>
 
    <add key="MailServer" value="Your Webmail"/>
    <add key="MailUserName" value="Sender MailId " />
    <add key="MailPassword" value="Password" />
  </appSettings>
  Permalink  
v4
Comments
Nelek at 7-Sep-13 8:12am
   
Did you realize that the question is more than 6 months old?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

http://www.c-sharpcorner.com/uploadfile/9f0ae2/send-emails-with-attachment-in-asp-net/
 
Visit this link....
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

sending mail
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
private void btnattachement_Click(System.Object sender, System.EventArgs e)
{
    OpenFileDialog1.ShowDialog();
 
}
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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 at 27-Feb-13 12:36pm
   
try sending some other file other than .ics
rahulkasar at 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)

  Print Answers RSS
0 OriginalGriff 7,215
1 DamithSL 5,114
2 Maciej Los 4,866
3 Sergey Alexandrovich Kryukov 4,747
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 13 Sep 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100