Monday, March 26, 2012

System.Net.Mail

I have a class that contains(taken from faq section):

using System.Net.Mail;

public class MailHelper
{
/// <summary>
/// Sends an mail message
/// </summary>
/// <param name="from">Sender address</param>
/// <param name="to">Recepient address</param>
/// <param name="bcc">Bcc recepient</param>
/// <param name="cc">Cc recepient</param>
/// <param name="subject">Subject of mail message</param>
/// <param name="body">Body of mail message</param>

public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
{
// Instantiate a new instance of MailMessage
MailMessage mMailMessage = new MailMessage();

// Set the sender address of the mail message
mMailMessage.From = new MailAddress(from);
// Set the recepient address of the mail message
//mMailMessage.To.Add(to);
//mMailMessage.To = new MailAddressCollection().Add(new MailAddress(to));

ERROR HERE: "Property or indexer 'System.Net.Mail.MailMessage.To' cannot be assisgned to--it is read only

mMailMessage.To = new MailAddressCollection().Add(to);

// Check if the bcc value is null or an empty string
if ((bcc != null) && (bcc != string.Empty))
{
// Set the Bcc address of the mail message
mMailMessage.Bcc.Add(new MailAddress(bcc));
}

// Check if the cc value is null or an empty value
if ((cc != null) && (cc != string.Empty))
{
// Set the CC address of the mail message
mMailMessage.CC.Add(new MailAddress(cc));
}

// Set the subject of the mail message
mMailMessage.Subject = subject;
// Set the body of the mail message
mMailMessage.Body = body;

// Secify the format of the body as HTML
mMailMessage.IsBodyHtml = true;
// Set the priority of the mail message to normal
mMailMessage.Priority = MailPriority.Normal;

// Instantiate a new instance of SmtpClient
SmtpClient mSmtpClient = new SmtpClient();
// Send the mail message
mSmtpClient.Send(mMailMessage);
}

I call the method from the Form Submit button Like this:

private void GenEmails()
{

//Email Code Executed On Click Event
string from = EmailTextBox.Text;
string to = ContactIdentifier.EmailAddress;
string bcc = "null";
string cc = "null";
string subject = subjectTextBox.Text;
string body = DescriptionTextBox.Text;

MailHelper.SendMailMessage(from, to, bcc, cc, subject, body);
}

All I'm trying to do is pass the textbox.text values into the method as parameters. What am I doing wrong here?

why don't you put just:

mMailMessage.To.Add(to);
//mMailMessage.To = new MailAddressCollection().Add(new MailAddress(to));

//ERROR HERE: "Property or indexer 'System.Net.Mail.MailMessage.To' cannot be assisgned to--it is read only

//mMailMessage.To = new MailAddressCollection().Add(to);

works for me...


// Set the recepient address of the mail message
//mMailMessage.To.Add(to);
//mMailMessage.To = new MailAddressCollection().Add(new MailAddress(to));

mMailMessage.To.Add(new MailAddress(to);

http://msdn2.microsoft.com/en-us/library/system.net.mail.mailmessage.to.aspx


Dim messageAsNew MailMessage("sender@.emailaddress.com", Session("destionationemailaddress"),"Order Alert","my e-mail body")

message.cc="sendanother@.emailaddresstothisaddress.com"

Dim emailClientAsNew SmtpClient("xxxxxxserver")

emailClient.Send(message)

when I add cc to the message object I get message.cc readonly error.

could you please help me with my problem.

thanks


use System.Web.Mail.MailMessage instead of System.Net.Mail.MailMessage.

0 comments:

Post a Comment