我想将带有多个附件的电子邮件发送给多个收件人/多个抄送收件人/密件抄送收件人,其中包含可能包含图像的格式化 html 内容男孩。
怎么做?
请建议。
这是从我的本地计算机向任何其他 id 发送 yahoo 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");
。我认为这行不通。你可以试一试。
通过使用 java 邮件 API,您可以实现这一点。
这里提供了非常清晰和简单的示例。最大化它足以解决问题...... :)