我想发送电子邮件,其中包含多个附件/多个CC接收者/ BCC接收者以及可能包含图像的格式化html内容男孩。
怎么做?
请建议。
答案 0 :(得分:5)
这是将yahoo id的电子邮件发送到本地计算机上的任何其他ID的工作示例。
package myWorkingFiles;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;
/**
*
* @author xyz
*/
public class YahooEmailWorkingWithAttachment {
public static void main(String[] args) {
String myEmailId = "zz@yahoo.com";
String myPassword = "myPass";
String senderId = "sfdsdf@gmail.com";
String ccId = "dddd@yahoo.com";
String bccId = "ffff@yahoo.com";
try {
MultiPartEmail email = new HtmlEmail();
email.setSmtpPort(587);
email.setAuthenticator(new DefaultAuthenticator(myEmailId, myPassword));
email.setDebug(true);
email.setHostName("smtp.mail.yahoo.com");
email.addTo(senderId);
email.addCc(ccId);
email.addBcc(bccId);
email.setFrom(myEmailId);
email.setSubject("Test Email");
email.setMsg("<font face='verdana' size='3'>Here is the test email in HTML format "
+ "<table>"
+ "<tr><th>id</th><th>Name</th></tr>"
+ "<tr><th>1</th><th>Name 1</th></tr>"
+ "<tr><th>2</th><th>Name 2</th></tr>"
+ "<tr><th>3</th><th>Name 3</th></tr>"
+ "<tr><th>4</th><th>Name 4</th></tr>"
+ "</table>"
+ "</font>");
// add the attachment
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("/Users/alkandari/Desktop/SMART/Fahim/test_small.pdf");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
email.attach(attachment);
attachment = new EmailAttachment();
attachment.setPath("/Users/alkandari/Desktop/SMART/Fahim/test.png");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
email.attach(attachment);
System.out.println("EmailAttachment.ATTACHMENT==" + EmailAttachment.ATTACHMENT);
// send the email
email.send();
System.out.println("email=====" + email + "==");
System.out.println("Mail sent!");
} catch (Exception e) {
System.out.println("Exception :: " + e);
}
}
}
如果您想从任何其他地方发送,您需要在以下两个地方进行更改
email.setSmtpPort(587);
email.setHostName("smtp.mail.yahoo.com");
我不确定,但如果您想将图片设置为电子邮件的背景,那么我相信您需要在服务器上实时显示图片,然后为<img src="server.com/images/myImage.png" />
提供服务器路径
要添加更多cc,您需要添加N个cc语句,如下所示。
email.addCc("id1@test.com");
email.addCc("id2@test.com");
email.addCc("id3@test.com");
email.addCc("id4@test.com");
我不确定email.addCc("id1@test.com, id2@test.com, id3@test.com");
。我认为它不会起作用。你可以尝试一下。
答案 1 :(得分:0)
通过使用java mail API,您可以实现此目的。
答案 2 :(得分:0)
提供了非常简单明了的例子here。 Max足以解决问题...:)