How to write Mime messages in C-sharp?

G

Guest

I have a Java code that I want to convert it to C-sharp in order to put it in my ASP.NET web application.

I used the JLCA (Java Language Conversion Assistant) for conversion, but it gave me many errors indicating that there are not matches in .NET for those Java classes!!

The Java code is:

*****************************************************************

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class Sender
{
public static void main(String [] args)
{
try
{
Properties props = System.getProperties();

props.put("mail.smtp.host","localhost");

Session session = Session.getDefaultInstance(props,null);

session.setDebug(true);

MimeMessage message = new MimeMessage(session);

message.setFrom(new InternetAddress("(e-mail address removed)","The user"));

message.addRecipient(Message.RecipientType.TO, new InternetAddress("(e-mail address removed)","User2"));

message.addHeader("Mime-Version","1.0");

MimeMultipart multipart = new MimeMultipart("related");

message.setContent(multipart);

message.writeTo(System.out);


MimeBodyPart messageBodyPart = new MimeBodyPart();

//Part 1

DataSource source = new FileDataSource("d:\\mmw\\real_logo.jpg");

messageBodyPart.setDataHandler(new DataHandler(source));

messageBodyPart.setHeader("Content-ID", "real_logo.jpg");

messageBodyPart.setFileName("real_logo.jpg");

multipart.addBodyPart(messageBodyPart);

//Part 2

messageBodyPart = new MimeBodyPart();

source = new FileDataSource("d:\\mmw\\hi.txt");

messageBodyPart.setDataHandler(new DataHandler(source));

messageBodyPart.setHeader("Content-ID", "hi.txt");

messageBodyPart.setFileName("hi.txt");

multipart.addBodyPart(messageBodyPart);


Transport.send(message);

}
catch(Exception e){System.err.println(""+e);}

}

}


*****************************************************************

Does anyone know the equivalent C-sharp code?
i.e, I need to make more than 1 mime part with different content-types and content-ids in one message!!

Or is there a software to convert my code to C-sharp directly?

Looking forward to hearing from you!!
HishHish
 
G

Guest

you have to use the

using System.Web.Mail

then create the object using the class MailMessage

e.g. MailMessage m_objMail = new MailMessage()

then use the properties as needed

Cheers
Jonathan Rucker
-- http://www.novaworks.com.au
 
G

Guest

Hi Jonathan

Thanks for your reply.

Actually I have been searching for appropriate classes and properties. I used MimePart, MailMessage, etc. but I couldn't find how to make different Mime parts with different Content-Types and Content-Ids within ONE message!!

Using the MimePart class, I couldn't find how to specify Content-Type and Content-Id for the MimePart object!

Also MailMessage body can only be set to a string!! So how can I set it to MimeMultipartRelated class or other Mime classes??

//HishHish
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top