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.
Suprotim Aga...
All-Star
15503 Points
1973 Posts
MVP
How to send an email with attachment in asp.net 2.0
Jul 31, 2007 02:09 AM|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
kaushalparik...
All-Star
26568 Points
3688 Posts
MVP
Re: How to send an email with attachment in asp.net 2.0
Jul 31, 2007 04:08 AM|LINK
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[KaushaL] || BloG || Twitter
Don't forget to click "Mark as Answer" on the post that helped you.
ronnieedin
Member
31 Points
111 Posts
Re: How to send an email with attachment in asp.net 2.0
Aug 07, 2007 10:03 AM|LINK
is there maximum size of attachment
Stoian Bucov...
Participant
1685 Points
265 Posts
Re: How to send an email with attachment in asp.net 2.0
Sep 15, 2007 09:59 PM|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
dotnet_lee
Member
467 Points
207 Posts
Re: How to send an email with attachment in asp.net 2.0
Oct 05, 2007 08:43 AM|LINK
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 Functionhandy01
Member
77 Points
23 Posts
Re: How to send an email with attachment in asp.net 2.0
Oct 15, 2007 02:08 PM|LINK
Please check on the link below
http://www.worldofasp.net/aspnet/Tutorials/Email_ASP.NET2/Sending_Email_in_ASP.NET_2.0_83.aspx
qwsoftdraw
Member
36 Points
48 Posts
Re: How to send an email with attachment in asp.net 2.0
Sep 19, 2009 03:35 PM|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.
mudassarkhan
All-Star
78956 Points
13402 Posts
MVP
Re: How to send an email with attachment in asp.net 2.0
Sep 19, 2009 05:13 PM|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
Contact me
qwsoftdraw
Member
36 Points
48 Posts
Re: How to send an email with attachment in asp.net 2.0
Sep 21, 2009 07:14 PM|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.
jrstafford1
Member
202 Points
60 Posts
Re: How to send an email with attachment in asp.net 2.0
Oct 06, 2009 07:55 PM|LINK
Thanks for the posts guys...this was very helpful!