Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在多部分邮件中设置HTML文本_Java_Email_Html Email_Apache Commons_Email Attachments - Fatal编程技术网

Java 在多部分邮件中设置HTML文本

Java 在多部分邮件中设置HTML文本,java,email,html-email,apache-commons,email-attachments,Java,Email,Html Email,Apache Commons,Email Attachments,我有java代码发送电子邮件附件如下 String myEmailId = "xx@yahoo.co.in"; String myPassword = "@xx"; String senderId = "yy@gmail.com"; try { MultiPartEmail email = new MultiPartEmail(); email.setSmtpPort(587); email.setAuthenticator(new DefaultAuthenticato

我有java代码发送电子邮件附件如下

String myEmailId = "xx@yahoo.co.in";
String myPassword = "@xx";
String senderId = "yy@gmail.com";
try {
    MultiPartEmail email = new MultiPartEmail();
    email.setSmtpPort(587);
    email.setAuthenticator(new DefaultAuthenticator(myEmailId, myPassword));
    email.setDebug(true);
    email.setHostName("smtp.mail.yahoo.com");
    email.addTo(senderId);
    email.setFrom(myEmailId);
    email.setSubject("The picture");
    email.setMsg("<font face='verdana' size='3'>Here is the picture you wanted "
            + "<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.pdf");
    attachment.setDisposition(EmailAttachment.ATTACHMENT);
    email.attach(attachment);

    // send the email
    email.send();
    System.out.println("Mail sent!");
} catch (Exception e) {
    System.out.println("Exception :: " + e);
}
String myEmailId=”xx@yahoo.co.in";
字符串myPassword=“@xx”;
字符串senderId=”yy@gmail.com";
试一试{
MultipartMail电子邮件=新的MultipartMail();
电邮:setsmtport(587);
setAuthenticator(新的DefaultAuthenticator(myEmailId,myPassword));
email.setDebug(true);
email.setHostName(“smtp.mail.yahoo.com”);
email.addTo(senderId);
email.setFrom(myEmailId);
email.setSubject(“图片”);
email.setMsg(“这是你想要的图片”
+ ""
+“idName”
+“1名称1”
+“2名称2”
+“3名称3”
+“4名称4”
+ ""
+ "");
//添加附件
EmailAttachment=新的EmailAttachment();
附件.setPath(“/Users/alkandari/Desktop/SMART/Fahim/test_small.pdf”);
附件.setDisposition(EmailAttachment.attachment);
电子邮件。附上(附件);
附件=新的电子邮件附件();
附件.setPath(“/Users/alkandari/Desktop/SMART/Fahim/test.pdf”);
附件.setDisposition(EmailAttachment.attachment);
电子邮件。附上(附件);
//发送电子邮件
email.send();
System.out.println(“邮件已发送!”);
}捕获(例外e){
System.out.println(“异常::”+e);
}
除了HTML代码按原样显示外,一切正常

在电子邮件中,我得到的是

<font face='verdana' size='3'>Here is the picture you wanted <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>
这是您想要的图片idName1Name 12Name 23Name 34Name 4
是否有任何参数,我将收到的电子邮件将有适当的HTML格式的数据

注意:

实际上我使用的是
Email=newsimpleemail()并在HTML部分完美工作的地方执行上述操作。然而,当我必须切换到附件时,我必须使用
MultiPartEmail email=newmultipartemail()我得到了答案

刚刚更改
MultiPartEmail=newmultipartemail()
多部分邮件电子邮件=新HtmlEmail()

我得到了答案


刚刚更改
MultiPartEmail=newmultipartemail()
多部分邮件电子邮件=新HtmlEmail()

您不能使用
email.setMsg()

但将消息正文设置为:


email.addPart(“MSG BODY your name”,“text/html;charset=UTF-8”)

您不能使用
email.setMsg()

但将消息正文设置为:


email.addPart(“MSG BODY your name”,“text/html;charset=UTF-8”)

我想在这里提供我的答案,以便未来的我(和其他人)可以看到完整的代码。无论出于什么原因,可能只是我,我发现这些答案不完整,或者它们对我不起作用。和OP一样,我试图发送一封带有PDF附件的基于HTML的电子邮件。以下是我使用commons email 1.4的结果。如有任何意见,将不胜感激

进口:

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.EmailAttachment;
构建你的电子邮件对象(显然这里的实际细节应该是你的)

现在您的信息详细信息。请注意,我的HTML包括HTML和BODY标记

email.setSubject("Your subject here");
email.addPart("<div>Your html here</div>", "text/html; charset=UTF-8");
现在发送电子邮件

email.send();

我想在这里提供我的答案,以便将来的我(和其他人)可以看到完整的代码。无论出于什么原因,可能只是我,我发现这些答案不完整,或者它们对我不起作用。和OP一样,我试图发送一封带有PDF附件的基于HTML的电子邮件。以下是我使用commons email 1.4的结果。如有任何意见,将不胜感激

进口:

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.EmailAttachment;
构建你的电子邮件对象(显然这里的实际细节应该是你的)

现在您的信息详细信息。请注意,我的HTML包括HTML和BODY标记

email.setSubject("Your subject here");
email.addPart("<div>Your html here</div>", "text/html; charset=UTF-8");
现在发送电子邮件

email.send();

您还需要有tag@MayankSharma:HTML,BODY标记是不需要的,因为
HtmlEmail()
本身就知道所写的内容都将在
标记内。您还需要tag@MayankSharma:HTML,BODY标记不需要作为
HtmlEmail()
本身理解,无论写什么都将在标签内。这非常重要。。不要将
email.addPart
email.setContent
一起使用,因为附件不会被发送!仅使用
email.addPart
非常重要。。不要将
email.addPart
email.setContent
一起使用,因为附件不会被发送!仅使用
email.addPart