Showing posts with label sends. Show all posts
Showing posts with label sends. Show all posts

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.

System.Net.Mail & sending emails with line breaks

Hi,

When a client sends an email via a web form (text entered in a asp:TextBox server control), the email received has no line breaks, and it is displayed in one line. This has bad impact on email readability... How can one overcome this probelm?

Thank you,

Alon

If you are sending mail text in simple text format then you have to use System.Environment.NewLine to move rest text in next line.

SmtpMail.SmtpServer = "1.0.0.127";
MailMessage mm = new MailMessage();
mm.From = "bill@.microsoft.com";
mm.To = "job@.apple.com";
mm.Subject = "";
mm.Body = "line 1" + System.Environment.NewLine + "line 2";


Hi,

Thank you for your reply.

I forgot to metion that the mail is sent in HTML format:

mailMessage.IsBodyHtml =true;

Am I supposed to parse the user's message (the text in the TextBox control), chck for CRLF (or is it \n) and replace it with System.Environment.NewLine? Isn't there some standard way to send the email, as i is in the text box (multiline text box. It looks like a text area)?

Thank you,

Alon


Here is the modified code for HTML formated body

mm.Body = "line 1" + "<br>" + "line 2";

Rest of the code will remain same.


But how can I know where line1 ends and line2 starts? All I have is a string I get form TextBox.Text. From this string I cannot deduce where did the user hit return - it's a "flat" string.

A word about he context of this: this web form is a "Contact Us" page. The TextBox is a multiline text box, which serves as a free text input field.

Thank you,

Alon


As what Firoz said, you have to use the "<br />"

You will use the following code to replace each line in your textbox to break

Replace(textbox1.text, vbCrLf,"<br />")

HTH
Regards


Oooops, now I understand. It works. I thank the both of you.

Alon

System.Net.Mail class smtpdemo needs users address in message body from txt.emailAddress

I have this code behind from the new asp.net 2.0 System.Net.Mail class smtpdemo i am working with. It sends an HTML email to myself the from address and to the email address in txt.emailAddress as a CC. I am trying to get the txt.email address somewhere in the message body that it sends out. this code does not show the imputed contents of txt.emailaddress anywhere in the email so i canot reply to the sender. It declaires me as the sender and sends the person filing out the form an email. I need to declar the contents of txt.emailadress as a variable and call it in the message body. Ive tryed dozens of different ways but they all have errors. i hope someone can look at this code and show me how to get the users email address sent with the message.
/code/
Imports System.Net.Mail

PartialClass report_aspx

Inherits System.Web.UI.Page

ProtectedSub sendEmail_Click(ByVal senderAsObject,ByVal e _

As System.EventArgs)Handles sendEmail.Click

Dim sBodyAsString

Dim fileReaderBodyAsString

Dim fileReaderFormatAsString

Dim WebDirectoryAsString = Server.MapPath(".") &"\"

Dim fileNameAsString = WebDirectory

Dim htmlEmailFormatAsString = WebDirectory &"MailFormat.htm"

SelectCase Subject.Text

Case"Broken Link"

fileName = fileName &"BrokenLink.txt"

Case"Missing Picture"

fileName = fileName &"MissingPicture.txt"

Case"Typographical Error"

fileName = fileName &"TypoError.txt"

CaseElse

fileName = fileName &"other.txt"

EndSelect

fileReaderBody =My.Computer.FileSystem.ReadAllText(fileName)

fileReaderFormat =My.Computer.FileSystem.ReadAllText(htmlEmailFormat)

sBody = fileReaderFormat.Replace("BODYGOESHERE", _

messageBody.Text)

sBody = sBody.Replace("CONTENTGOESHERE", fileReaderBody)

sBody = sBody.Replace("SUBJECTGOESHERE", Subject.Text)

Dim toAddressAs MailAddress =New _

MailAddress(me@dotnet.itags.org.myemailaddress,"Webmaster")

Dim ccAddressAs MailAddress =New _

MailAddress(emailAddress.Text, emailName.Text)

Dim SenderAddressAs MailAddress =New _

MailAddress(emailAddress.Text)

Dim fromAddressAs MailAddress =New _

MailAddress(me@dotnet.itags.org.myemailaddress)

Dim eMailAs MailMessage =New _

MailMessage(fromAddress, fromAddress)

eMail.CC.Add(fromAddress)

eMail.Subject = Subject.Text

Dim htmlTypeAs System.Net.Mime.ContentType = _

New System.Net.Mime.ContentType("text/html")

Dim txtTypeAs System.Net.Mime.ContentType = _

New System.Net.Mime.ContentType("text/plain")

eMail.AlternateViews.Add( _

AlternateView.CreateAlternateViewFromString(sBody, htmlType))

eMail.AlternateViews.Add( _

AlternateView.CreateAlternateViewFromString(fileReaderBody & vbCrLf & messageBody.Text, htmlType))

Dim attchmentAs Attachment =New _

Attachment(WebDirectory &"goldbar.gif")

eMail.Attachments.Add(attchment)

Dim clientAs SmtpClient =New SmtpClient("myemailserver")

client.Send(eMail)

lblMessageSent.Text ="MESSAGE SENT"

emailAddress.Text =""

emailName.Text =""

messageBody.Text =""

EndSub

ProtectedSub Page_Load(ByVal senderAsObject,ByVal eAs System.EventArgs)HandlesMe.Load

EndSub

EndClass
/end code/

I fixed my form by modifying the mailformat.htm page that I will place below and adding:

/codebehind file/
Body = sBody.Replace("NAMEGOESHERE", emailName.Text)

sBody = sBody.Replace("EMAILFROMGOESHERE", emailAddress.Text)
/end codebehind file/
/mailformat.html/

<html><body><h1>SUBJECTGOESHERE</h1><imgsrc="goldbar.gif"/><br/>BODYGOESHERE

<br/>

<imgsrc="goldbar.gif"/><br/>

NAMEGOESHERE<br/>

EMAILFROMGOESHERE<br/>

<br/>

<blockquote>CONTENTGOESHERE</blockquote></body></html>

system.net.mail network host cache?

I need to change the name of the mail server for my .NET application. I changed it in web.config and on the .aspx page that sends the message. However, I still get an error message saying that the remote name could not be resolved, and the error message has the name of theold mail server. I have tried rebooting the server, but still get the same error.

Does .NET save configuration settings somewhere other than web.config? Is something getting cached? Other ideas?

Are you running VS debugger or testing on IIS server?

You can delete all files in cache.

Do you use only one config file?

If you have BLL and DAL, sometimes you have also config files here.


Visit this?site.
http://www.systemwebmail.com/

As Johan said, delete your temporary files, and build your web site again

Thanks


Thanks for the responses.

JohanNL, this site is on an IIS server. There is only one web.config file for the site. I did not implement BLL/DAL.

JohanNL and e_screw - this is probably a dumb question - when you say to delete cache/temp files, do you mean the ones in c:\windows\temp? Or what is the correct location?


You need to clear the ASP.NET temporary files

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files

Thanks