I've just enabled mailing to multiple recipients using Ryan's suggestion of a param array. Here's how to quickly modify your code to make this work in
VB.NET.
1. Modify the Code File - mailHelper.vb
Update the first few lines of the SendMailMessage subroutine as follows. Make the ParamArray the final parameter passed. Then loop through the ParamArray, adding each string (email address) as a recipient.
==================================
Public Shared Sub SendMailMessage(ByVal from As String, ByVal bcc As String, ByVal cc As String, _
ByVal subject As String, ByVal body As String, ByVal ParamArray strTo() As String)
' Instantiate a new instance of MailMessage
Dim mMailMessage As New MailMessage()
' Set the sender address of the mail message
mMailMessage.From = New MailAddress(from)
' Set the recepient addresses of the mail message
For Each s As String In strTo
mMailMessage.To.Add(New MailAddress(s))
Next
==================================
2. Pass Multiple Addresses into the ParamArray
Anywhere you call the MailHelper, you can pass in multiple recipients as follows.
==================================
' Prepare the email
Dim strTo() As String = {"Recipient1@email.xyz", "Recipient2@email.xyz"}
Dim strFrom, strBcc, strCC, strSubject, strBody As String
strFrom = "me@email.xyz"
strBcc = "bccgal@email.xyz"
strCC = "ccguy@email.xyz"
strSubject = "This is the message subject."
strBody = "This ticket the message body."' Send the email
MailHelper.SendMailMessage(strFrom, strBcc, strCC, strSubject, strBody, strTo)
==================================
Ryan, your post was extremely helpful. Thanks a lot!
Hi.would like to know if I can set a friendly name for the e-mail
from attribute, so that users will see that friendly name in their incoming box instead of the raw e-mail address. I wonder if that can be set in the web.config file in the
<smtp> line or if my only choice is to set it up in the MailMessage object. Thanks a lot.
This code works great for sending an email, but how would you add a file attachment t the email? I've tinkered around with the mailhelper class but can't get it to work. Any suggestions?
StrongTypes, I thank you for posting this. I'm having trouble getting starting as I am new to development in general.
I have tried several ways to call the function SendMailMessage with no success. So I have a couple of questions and I would appreciate a response for a relative beginner if you have the time:
I've placed your file in my app_code folder. How does a page know to look for it there? Is there a line that I need to add at the top of the page to tell it look in that file in the app_code folder?
After trying unsuccessfully to call it from inside the app_code folder I gave up and tried copying it in a script inside the page, (I'm trying to use a button and the onclick="Mailhelper.SendMailMessage()" with the parameters filled in to call it). I then
get the following error:
Server Error in '/' Application.
Parser Error
Description:
An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: The server tag is not well formed.
Source Error:
Line 200: <tr> Line 201: <td colspan="2"> Line 202: <Club:RolloverButton ID="Newsletter" runat="server" Text="Submit" onclick="SendMailMessage("test@theirdomain","test@mydomain.com","","","Newsletter Request", "NewsletterName")"/> Line 203: </td> Line 204: </tr>
Source File: /Join2.aspx Line: 202
Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.210
If anyone can give a couple of the more basic steps for using this I would greatly appreciate it, (and it looks like there are quite a few of the others who would too).
I've got a FormView for users to enter their data. That works fine. But I'd also like to send them a confirmation e-mail with the data they've just entered. How would I do this?
I know how to create a Mail Message with simple textboxes. But in my example the FormView complicates matters! Can anyone help me? I'm getting quite frustrated...
gsdtech
Participant
1227 Points
190 Posts
Re: HOW TO: Send email using System.Net.Mail
Jan 16, 2007 05:11 PM|LINK
I've just enabled mailing to multiple recipients using Ryan's suggestion of a param array. Here's how to quickly modify your code to make this work in VB.NET.
1. Modify the Code File - mailHelper.vb
Update the first few lines of the SendMailMessage subroutine as follows. Make the ParamArray the final parameter passed. Then loop through the ParamArray, adding each string (email address) as a recipient.
==================================
Public Shared Sub SendMailMessage(ByVal from As String, ByVal bcc As String, ByVal cc As String, _ ByVal subject As String, ByVal body As String, ByVal ParamArray strTo() As String) ' Instantiate a new instance of MailMessage Dim mMailMessage As New MailMessage() ' Set the sender address of the mail message mMailMessage.From = New MailAddress(from) ' Set the recepient addresses of the mail message For Each s As String In strTo mMailMessage.To.Add(New MailAddress(s)) Next==================================2. Pass Multiple Addresses into the ParamArray
Anywhere you call the MailHelper, you can pass in multiple recipients as follows.
==================================
aus_nexxus
Star
8018 Points
1156 Posts
Re: HOW TO: Send email using System.Net.Mail
Jan 17, 2007 06:32 AM|LINK
Check out what we did on the DotNetNuke project for handling bulk email. Wwe have some good code on threading and sending lots of mail.
Cheers
mabho
Member
21 Points
11 Posts
Re: HOW TO: Send email using System.Net.Mail
Jan 21, 2007 07:29 PM|LINK
will9key
Member
114 Points
44 Posts
Re: HOW TO: Send email using System.Net.Mail
Jan 24, 2007 02:06 PM|LINK
Hi folks,
This code works great for sending an email, but how would you add a file attachment t the email? I've tinkered around with the mailhelper class but can't get it to work. Any suggestions?
Thanks
Will
will9key
Member
114 Points
44 Posts
Re: HOW TO: Send email using System.Net.Mail
Jan 24, 2007 02:37 PM|LINK
mletsch
Member
35 Points
10 Posts
Re: HOW TO: Send email using System.Net.Mail
Jan 28, 2007 02:57 PM|LINK
StrongTypes, I thank you for posting this. I'm having trouble getting starting as I am new to development in general.
I have tried several ways to call the function SendMailMessage with no success. So I have a couple of questions and I would appreciate a response for a relative beginner if you have the time:
Server Error in '/' Application.
Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.Parser Error Message: The server tag is not well formed.
Source Error:
Source File: /Join2.aspx Line: 202
Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.210
If anyone can give a couple of the more basic steps for using this I would greatly appreciate it, (and it looks like there are quite a few of the others who would too).
Thanks so much!
Ritesh Mangl...
Participant
835 Points
193 Posts
Re: HOW TO: Send email using System.Net.Mail
Jan 31, 2007 05:28 AM|LINK
I am getting the similar error. Any help on this.
Thanks
Ritesh
jazeelkm
Member
28 Points
30 Posts
Re: HOW TO: Send email using System.Net.Mail
Feb 09, 2007 09:19 AM|LINK
thanx, nice code
How can i send a word document(say abc.doc) as an attachament using System.Net.Mail
StrongTypes
All-Star
30801 Points
6013 Posts
ASPInsiders
Re: HOW TO: Send email using System.Net.Mail
Feb 09, 2007 07:08 PM|LINK
See http://www.systemnetmail.com/faq/2.3.aspx.
Subliminal V...
Member
604 Points
195 Posts
Re: HOW TO: Send email using System.Net.Mail
Feb 13, 2007 02:34 PM|LINK
Hi,
I've got the following mail-related question:
I've got a FormView for users to enter their data. That works fine. But I'd also like to send them a confirmation e-mail with the data they've just entered. How would I do this?
I know how to create a Mail Message with simple textboxes. But in my example the FormView complicates matters! Can anyone help me? I'm getting quite frustrated...
Thanks a lot,
Jerome