我正在使用javax.mail-1.6.2.jar,我制作了一个外部使用的类,我想在其中发送邮件。主要问题在代码的这一部分:
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
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;
import org.apache.log4j.Logger;
import org.json.JSONObject;
import main.java.caches.ServerCache;
import main.java.caches.servercache.MailConfig;
public class Mailer {
private static final Logger LOG = Logger.getLogger(Mailer.class.getName());
private static final String DEFAULTCONTENTTYPE = "text/html";
private MailConfig mailConfig;
private String mailSender;
private String host;
private String topic;
private String message;
private String contentType;
private List<String> recipients = new ArrayList<>();
private List<String> cc = new ArrayList<>();
private List<String> bcc = new ArrayList<>();
private List<File> attachments = new ArrayList<>();
public Mailer() {
Map<String, JSONObject> otherConfigs = ServerCache.getCache().getSysConf().getOtherConfigs();
if(!otherConfigs.containsKey("MAIL")) {
LOG.error("Keine Konfiguration für MAIL verfügbar");
throw new RuntimeException();
}
mailConfig = ServerCache.getCache().getSysConf().getMailConfig();
if(!mailConfig.isValid()) {
LOG.error("MAIL Konfiguration unvollständig");
throw new RuntimeException();
}
mailSender = mailConfig.getSender();
host = mailConfig.getServer();
setContentType(DEFAULTCONTENTTYPE);
}
private Session getSessionForMail() {
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
properties.put("mail.smtp.port", "25");
Session session = Session.getDefaultInstance(properties);
return session;
}
public void sendeMail() {
if(topic == null) {
throw new RuntimeException("Betreff fehlt");
}
if(recipients.size() == 0) {
throw new RuntimeException("Keine Empfänger");
}
try {
Session session = getSessionForMail();
Message mail = createMessage(session);
Multipart mimeMultipart = makeMultipart();
mail.setContent(mimeMultipart);
mail.setContent(message , contentType);
Transport.send(mail);
LOG.info("Die Mail wurde an folgende Mailadresse(n) versandt: " + Arrays.toString(mail.getAllRecipients()));
} catch (MessagingException mex) {
LOG.error("Beim Versenden der Mail ist eine MessagingException aufgetreten: " + mex.getMessage(), mex);
}
}
private Multipart makeMultipart() throws MessagingException {
Multipart mimeMultipart = new MimeMultipart();
for (File a : attachments) {
try {
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
attachmentBodyPart.attachFile(a);
attachmentBodyPart.setFileName(a.getName());
mimeMultipart.addBodyPart(attachmentBodyPart);
} catch (IOException e) {
LOG.error("Datei: " + a.getName() + "wurde nicht gefunden.");
}
}
BodyPart textBodyPart = new MimeBodyPart();
textBodyPart.setContent(message , contentType);
mimeMultipart.addBodyPart(textBodyPart);
return mimeMultipart;
}
private MimeMessage createMessage(Session session) throws AddressException, MessagingException {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(mailSender));
for (String r : recipients) {
message.addRecipient(Message.RecipientType.TO, new InternetAddress(r));
}
for (String r : cc) {
message.addRecipient(Message.RecipientType.CC, new InternetAddress(r));
}
for (String r : bcc) {
message.addRecipient(Message.RecipientType.BCC, new InternetAddress(r));
}
message.setSubject(topic);
return message;
}
public void setTopic(String topic) { this.topic = topic; }
public void setMessage(String message) { this.message = message; }
public void setRecipients(List<String> recipients) { this.recipients = recipients; }
public void setAttachments(List<File> attachments) { this.attachments = attachments; }
public void setCC(List<String> cc) { this.cc = cc; }
public void setBCC(List<String> bcc) { this.bcc = bcc; }
public void setContentType(String contentType) { this.contentType = contentType; }
}
在这个配置中,我在电子邮件中收到文本,因为我覆盖了mime内容。如果我删除mail.setContent(消息,contentType);
行,我的邮件将保持为空。所以contetnt类型和消息变量完全正常,只有Multipart无法正常工作。我在调试时看到,目前我设置了body part的内容,但内容为空,并且dh(datahandler)设置了一个对象。
编辑
现在你有了整个类。我认为举例子应该不是问题。设置文本或消息对象的内容很好,但如果我将multipart设置为消息对象的内容,文本将保持为空。
尝试替换方法中的以下代码
BodyPart textBodyPart = new MimeBodyPart();
textBodyPart.setContent(message , contentType);
具有
BodyPart textBodyPart = new MimeBodyPart();
((MimeBodyPart)textBodyPart).setText(message, contentType);
代码如下: 这将向flask发送一个{“name”:“go”}JSON字典。Flask应该将语言名称追加到数组中,并在响应中返回完整的数组。现在,当我手动发送请求时,这就工作了,所以这不是Flask的错误。但是当我从iOS发送上述内容时,我在flask控制台中得到request.json==None。很明显,我送的是一具空尸体,但我不应该。你知道我哪里出了问题吗?
问题内容: 我有一条发送pdf文件的路线: 我尝试使用另一种解决方案,它们执行或多或少的相同功能: 和 我的问题是,当我在浏览器中询问此路由时,收到的空pdf文件的页面数与原始文件的页面数相同。 我使用配置了Express Server 。 有人可以帮助我吗? 问题答案: 在使用connect-livereload中间件时,我已经看到了这一点。问题在于connect- livereload正在将j
我有一个 这个数组由保存的字符串和核心数据组成。我想通过空投发送这个。我已经签出了,并且我试图通过以下方式发送它 这给了我以下错误“NSInvalidArgumentException”,原因是:“JSON写入(证书)中的类型无效” 我已经将数据转换为字符串,所以不确定我在这里错过了什么 我研究了NSInvalidArgumentException,原因:“JSON写入中的类型无效(u NSDat
我想给我的频道发送一条嵌入信息,所以当我打字的时候 在我的频道中,它应该返回嵌入消息,如 但是它只是从我的testbot(机器人名称)返回一个空消息。我试过用 message.channel.send(嵌入,嵌入); 相反,但是它给我一个错误,说嵌入没有声明...发送(内容,选项)是格式,嵌入是选项。 我的代码在上面,有什么问题吗?将var更改为const也没有任何作用。
以下是我如何发送: 或者像这样(两者都试过): 在接收端,如果我访问$\u POST,则它是空的。如果我访问文件\u获取\u内容('php://input“),然后它包含我的json。 这是怎么回事?