使用Java代码发送电子邮件 [英] sending an email using java code
本文介绍了使用Java代码发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想以html格式发送电子邮件,我想获得示例代码
谁能帮我
i want send an email in the form of html format i want sample code
can any one help me
推荐答案
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class MailClient
{
public void sendMail(String mailServer, String from, String to,
String subject, String messageBody,
String[] attachments) throws
MessagingException, AddressException
{
// Setup mail server
Properties props = System.getProperties();
props.put("mail.smtp.host", mailServer);
// Get a mail session
Session session = Session.getDefaultInstance(props, null);
// Define a new mail message
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
// Create a message part to represent the body text
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(messageBody);
//use a MimeMultipart as we need to handle the file attachments
Multipart multipart = new MimeMultipart();
//add the message body to the mime message
multipart.addBodyPart(messageBodyPart);
// add any file attachments to the message
addAtachments(attachments, multipart);
// Put all message parts in the message
message.setContent(multipart);
// Send the message
Transport.send(message);
}
protected void addAtachments(String[] attachments, Multipart multipart) throws MessagingException, AddressException
{
for(int i = 0; i<= attachments.length -1; i++)
{
String filename = attachments[i];
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
//use a JAF FileDataSource as it does MIME type detection
DataSource source = new FileDataSource(filename);
attachmentBodyPart.setDataHandler(new DataHandler(source));
//assume that the filename you want to send is the same as the
//actual file name - could alter this to remove the file path
attachmentBodyPart.setFileName(filename);
//add the attachment
multipart.addBodyPart(attachmentBodyPart);
}
}
public static void main(String[] args)
{
try
{
MailClient client = new MailClient();
String server="mymail.com";
String from="abc@mymail.com";
String to = "xyz@yourmail.com";
String subject="Testing";
String message="This is a testing mail";
String[] filenames = {"c:/myfile.txt"};
client.sendMail(server,from,to,subject,message,filenames);
}
catch(Exception e)
{
e.printStackTrace(System.out);
}
}
}
您可以使用此代码并替换您的服务器设置.
希望对您有所帮助:)
You can use this code and Replace you server settings.
hope it helps :)
检查本教程:
http://java.sun.com/developer/onlineTraining/JavaMail/contents.html [ ^ ]
很好,全都在那里.
如果您需要电子邮件服务器,请尝试使用hmail: http://www.hmailserver.com/ [
check this tutorial:
http://java.sun.com/developer/onlineTraining/JavaMail/contents.html[^]
pretty good, it''s all in there.
If you need a email server try hmail: http://www.hmailserver.com/[^]
这篇关于使用Java代码发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文