code to Send Mail:
C#
using System.Net;
using System.Net.Mail;
-----------------------
public static bool SendMail(string strFrom, string strTo, string strSubject, string strMsg)
{
try
{
// Create the mail message
MailMessage objMailMsg = new MailMessage(strFrom, strTo);
objMailMsg.BodyEncoding = Encoding.UTF8;
objMailMsg.Subject = strSubject;
objMailMsg.Body = strMsg;
objMailMsg.Priority = MailPriority.High;
objMailMsg.IsBodyHtml = true;
//prepare to send mail via SMTP transport
SmtpClient objSMTPClient = new SmtpClient();
objSMTPClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
objSMTPClient.Send(objMailMsg);
return true;
}
catch (Exception ex)
{
throw ex;
}
}
VB:
Public Shared Function SendMail(ByVal strFrom As String, ByVal strTo As String, ByVal strSubject As String, ByVal strMsg As String) As Boolean
Try
' Create the mail message
Dim objMailMsg As MailMessage = New MailMessage(strFrom, strTo)
objMailMsg.BodyEncoding = Encoding.UTF8
objMailMsg.Subject = strSubject
objMailMsg.Body = strMsg
objMailMsg.Priority = MailPriority.High
objMailMsg.IsBodyHtml = True
'prepare to send mail via SMTP transport
Dim objSMTPClient As SmtpClient = New SmtpClient()
objSMTPClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis
objSMTPClient.Send(objMailMsg)
Return True
Catch ex As Exception
Throw ex
End Try
End Function
Code to send mail with attechment.,
public static bool SendMail(string strFrom, string strTo, string strSubject, string strMsg) { try
{
// Create the mail message
MailMessage objMailMsg = new MailMessage(strFrom, strTo);
Here is how I use to send e-mail from the web feedback form (the code is VB), and I am using server credentials:
Imports System.Net.Mail
Partial Class ContactsEnglish
Inherits System.Web.UI.Page
Protected Sub send_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles send.Click
If Page.IsValid() Then
Dim mySmtpClient As New SmtpClient
Dim myMail As New MailMessage
Dim myFrom As New MailAddress(email.Text)
Dim myTo As New MailAddress("recipients_email@server.com")
myMail.BodyEncoding = System.Text.Encoding.UTF8
myMail.To.Add(myTo)
myMail.From = myFrom
myMail.Subject = "Mail's subject goes here"
myMail.Body = vbCrLf & "Country: " & country.Text & vbCrLf & vbCrLf & "Name: " & name.Text & vbCrLf & vbCrLf & "Phone number: " & phone.Text & vbCrLf & vbCrLf & "E-mail: " & email.Text & vbCrLf & vbCrLf & "Inquiry: " & inquiry.Text
mySmtpClient.Host = "www.YourMailServer.com"
mySmtpClient.Credentials = New System.Net.NetworkCredential("YourMail@server.com", "YourMailPassword", "www.YourMailServer.com")
Try
mySmtpClient.Send(myMail)
lbl.Text = "Your message has been sent. Thank you !"
Catch ex As Exception
lbl.Text = "Sending failed !"
End Try
End If
End Sub
End Class
P.S.
This mail will be sent in text format. The asp's controls that I am using are 5 text boxes, button send and validation controls for the requred field.
Hope it helps
Regards
Please, don't forget to mark as "Answered" if it helped you. Thanks.
Heres another one - In ASP.NET 2.0 using VB also attaching a file like shown above
Public Shared Function SendMail(ByVal strFrom As String, ByVal strTo As String, ByVal strSubject As String, ByVal strMessage As String) As Boolean
Try
Dim MySmtpClient As New System.Net.Mail.SmtpClient("Your SMTP Server")
MySmtpClient.Credentials = New System.Net.NetworkCredential("Your-username", "Your-password")
Dim msg As New System.Net.Mail.MailMessage(strFrom, strTo, strSubject, strMessage)
msg.BodyEncoding = Encoding.UTF8
msg.IsBodyHtml = True
msg.Priority = Net.Mail.MailPriority.High
'Add an Attachment
Dim at As New System.Net.Mail.Attachment(Current.Server.MapPath("~/Uploaded/txt.doc"))
msg.Attachments.Add(at)
MySmtpClient.Send(msg)
Return True
Catch ex As Exception
Throw ex
End Try
End Function
Is there a way using ASP.net 2.0 and visual basic that a person can rename an attachment before it reaches the person your sending it to by email. For example, I want to send an email with an attachment from my website. I have been able to send an email
with an attachment from a website successfully, but when I receive the email, the attachment not only has the original name of the file, but it also contains the name of the web path. For instance, if the file is Test.zip and it's located on a web server D:\Base\mywebname.com\TestFolder,
then the zip file shows up in my email as: _Base_mywebname.com_TestFolder_Test.zip.
I do not want the path name to be viewable by the person who receives the attachment by email. Is there a way to keep the path from showing up in the final attachment file name?
For some reason now on my website when I use the same code I had been using, it sends just the name of the attached file. I have to wonder if my website company caught the error and corrected it themselves.
Anyways...what I want done is working now.
Trying the code you gave me just gave me Security Errors, but again, thank you for responding.
Star
13714 Points
1961 Posts
MVP
How to send an email with attachment in asp.net 2.0
Jul 30, 2007 10:09 PM|Suprotim Agarwal|LINK
http://www.dotnetcurry.com/ShowArticle.aspx?ID=65
HTH,
Suprotim Agarwal
Email Attachment ASP.NET 2.0 SMTP
Free Magazine for ASP.NET Developers | ASP.NET MVC Tuts | ASP.NET Tuts
All-Star
31362 Points
7055 Posts
Re: How to send an email with attachment in asp.net 2.0
Jul 31, 2007 12:08 AM|kaushalparik27|LINK
[KaushaL] Blog Twitter [MS MVP 2008 & 2009] [MCC 2011] [MVP Reconnect 2017]
Don't forget to click "Mark as Answer" on the post that helped you
Member
21 Points
107 Posts
Re: How to send an email with attachment in asp.net 2.0
Aug 07, 2007 06:03 AM|ronnieedin|LINK
is there maximum size of attachment
Participant
1254 Points
329 Posts
Re: How to send an email with attachment in asp.net 2.0
Sep 15, 2007 05:59 PM|Stoian Bucovich|LINK
Here is how I use to send e-mail from the web feedback form (the code is VB), and I am using server credentials:
Imports System.Net.Mail
Partial Class ContactsEnglish
Inherits System.Web.UI.Page
Protected Sub send_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles send.Click
If Page.IsValid() Then
Dim mySmtpClient As New SmtpClient
Dim myMail As New MailMessage
Dim myFrom As New MailAddress(email.Text)
Dim myTo As New MailAddress("recipients_email@server.com")
myMail.BodyEncoding = System.Text.Encoding.UTF8
myMail.To.Add(myTo)
myMail.From = myFrom
myMail.Subject = "Mail's subject goes here"
myMail.Body = vbCrLf & "Country: " & country.Text & vbCrLf & vbCrLf & "Name: " & name.Text & vbCrLf & vbCrLf & "Phone number: " & phone.Text & vbCrLf & vbCrLf & "E-mail: " & email.Text & vbCrLf & vbCrLf & "Inquiry: " & inquiry.Text
mySmtpClient.Host = "www.YourMailServer.com"
mySmtpClient.Credentials = New System.Net.NetworkCredential("YourMail@server.com", "YourMailPassword", "www.YourMailServer.com")
Try
mySmtpClient.Send(myMail)
lbl.Text = "Your message has been sent. Thank you !"
Catch ex As Exception
lbl.Text = "Sending failed !"
End Try
End If
End Sub
End Class
P.S.
This mail will be sent in text format. The asp's controls that I am using are 5 text boxes, button send and validation controls for the requred field.
Hope it helps
Regards
Member
20 Points
194 Posts
Re: How to send an email with attachment in asp.net 2.0
Oct 05, 2007 04:43 AM|dotnet_lee|LINK
Heres another one - In ASP.NET 2.0 using VB also attaching a file like shown above
Member
17 Points
66 Posts
Re: How to send an email with attachment in asp.net 2.0
Sep 19, 2009 11:35 AM|qwsoftdraw|LINK
Is there a way using ASP.net 2.0 and visual basic that a person can rename an attachment before it reaches the person your sending it to by email. For example, I want to send an email with an attachment from my website. I have been able to send an email with an attachment from a website successfully, but when I receive the email, the attachment not only has the original name of the file, but it also contains the name of the web path. For instance, if the file is Test.zip and it's located on a web server D:\Base\mywebname.com\TestFolder, then the zip file shows up in my email as: _Base_mywebname.com_TestFolder_Test.zip.
I do not want the path name to be viewable by the person who receives the attachment by email. Is there a way to keep the path from showing up in the final attachment file name?
Thanks in advance.
All-Star
54508 Points
14111 Posts
Re: How to send an email with attachment in asp.net 2.0
Sep 19, 2009 01:13 PM|mudassarkhan|LINK
Refer here I have explained the same
http://www.aspsnippets.com/post/2009/08/04/Contact-Us-Form-with-Rich-TextBox-in-ASPNet.aspx
Blog: ASPSnippets | Forum: ASPForums | Company: Excelasoft
Member
17 Points
66 Posts
Re: How to send an email with attachment in asp.net 2.0
Sep 21, 2009 03:14 PM|qwsoftdraw|LINK
Thank you for responding.
For some reason now on my website when I use the same code I had been using, it sends just the name of the attached file. I have to wonder if my website company caught the error and corrected it themselves.
Anyways...what I want done is working now.
Trying the code you gave me just gave me Security Errors, but again, thank you for responding.
Member
126 Points
63 Posts
Re: How to send an email with attachment in asp.net 2.0
Oct 06, 2009 03:55 PM|jrstafford1|LINK
Thanks for the posts guys...this was very helpful!
None
0 Points
2 Posts
Re: How to send an email with attachment in asp.net 2.0
Aug 03, 2013 06:08 AM|ranadheerk|LINK
This is code to send email with attachement
reference: http://coding-issues.com/2012/11/sending-email-with-attachments-from-c.html
Email Attachment ASP.NET 2.0 SMTP