1. Original Entry + Comments2. Write a Comment3. Preview Comment
New comments for this entry are disabled.


October 14, 2003  |  Extending System.Web.Mail  |  16259 hit(s)

A little preview: at work today, someone noted to me that in .NET 1.1, they added support for CDO fields to my good friend the System.Web.Mail class. The theory is that this will allow us to set/pass values that are not directly exposed via properties on the MailMessage class. The canonical example goes like this:
MailMessage m = new MailMessage(); 
m.From = "..."; // fill in appropriately
m.To = "..."; // fill in appropriately
m.Subject = "Subject";
m.Body= "test " + DateTime.Now.ToString();
m.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
m.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = user;
m.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = password;
SmtpMail.Send(m);
And in fact, mharder had a a post about this on the forums a while back. (Who knew? Not me.)

The example illustrates the solution to a popular question: how to pass authentication credentials with an email message if the server requires logon. I got this working tonight on my local SMTP server. The smtpauthenticate field is set to 1 in the example, which per the CdoProtocolsAuthentication enum indicates Basic. So I set my SMTP server to require Basic auth, turned off anonymous access, and gave it a try. Worked!

According to the docs, you're supposed to be able to set other fields as well. The other one that really interests me is setting the Reply-To field, another question I've been asked. Alas, no luck. I'm trying this:
m.Fields("http://schemas.microsoft.com/cdo/configuration/senduserreplyemailaddress") _
= "Mike Pope <mikepope@myotherdomain.com>"
(In VB, of course.) The field and value are accepted (changing the field name to "foo" throws), and the email gets sent. However, when I grub around in the message details, there's no reply-to info anywhere. So I don't know what CDO is doing with this value that I'm setting.

Well, perhaps that will solve itself.


Update, 1:36am Well, shoot -- thanks to Colt (see comment), we also now have Reply-To figured out. Basically, we now know that the Headers property, which is a simple dictionary, will take any header value (that is, anything documented here). By golly, I've learned two things today, and it's barely past midnight.




Colt   14 Oct 03 - 1:24 AM

Hey Mike, What's a great blog!
(I learn something new today)

Will you update your Email Sending in ASP.NET FAQ on the Forum? I'm sure that's a very good tip for all of the ASP.NET developers.

BTW, you may try the following code in order to change the Reply-To field per MailMessage object:

Dim mailMessage As System.Web.Mail.MailMessage = New System.Web.Mail.MailMessage

mailMessage.From = "Sender@ISP.com"
mailMessage.Headers.Add("Reply-To", "YourReplyToAddress@ISP.com")
mailMessage.To = "Receipient@ISP.com"
mailMessage.Subject = "Email Subject"
mailMessage.BodyFormat = System.Web.Mail.MailFormat.Html
mailMessage.Body = "Blah"

System.Web.Mail.SmtpMail.SmtpServer = "MailServerHere"
System.Web.Mail.SmtpMail.Send(mailMessage)

Colt


 
Brian Desmond   14 Oct 03 - 4:05 PM

Mike-

Thanks for the excellent information!

--Brian Desmond


 
Mike Poole   20 Feb 05 - 12:14 PM

Just to let you know that this post was extremely useful to me today! Btw, your blog makes a refreshing read.

 
Vivek   17 Mar 05 - 10:07 PM

Hi,

How can I display my name along with my mail id to the recipent.

say: Vivek(vivek.pareek@accenture.com)

Regards,

Vivek


 
Dejan   18 Jul 05 - 5:33 AM

Hi,
I've been reading about this new feature in your BLOG and I just want to ask one question. Can I add inline image by using the fields or headers collection.
Thanks


 
mike   18 Jul 05 - 8:34 AM

Dejan: unfortunately, you can't embed images inline in .NET 1.1, as noted here by Dave Wanta:

http://www.systemwebmail.com/faq/3.3.aspx



 
Simone Busoli   10 Dec 05 - 11:53 PM

DotNetOpenMail (http://dotnetopenmail.sourceforge.net/) is a great library for sending emails with ASP.NET 1.1, and it's open-source. It allows for inline-graphics as well as for a billion other things!

 
Roopal   07 Feb 06 - 7:20 AM

How to use mailMessage.Headers.Add to get a delivery report?
Please dont suggest
mailMessage.Headers.Add("Return-Receipt-To", "SomeID@cvc.com" )
mailMessage.Headers.Add("Disposition-Notification-To", "SomeID@fgft.com" )
as it is for read receipt .


 
mike   07 Feb 06 - 8:47 AM

Roopal, try posting your question on the ASP.NET forums:

http://forums.asp.net/