gpt4 book ai didi

java - 发送和接收来自同一类(class)的邮件?

转载 作者:行者123 更新时间:2023-12-01 15:40:23 24 4
gpt4 key购买 nike

现在只是测试一下,但是您可以发送和接收来自下面同一类的邮件吗?

package com.mailserver;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

@SuppressWarnings("serial")
public class MailHandlerServlet extends HttpServlet {
public static final Logger _log = Logger.getLogger(MailHandlerServlet.class.getName());

@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

sendmail();

try {

Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session, req.getInputStream());

//Extract out the important fields from the Mime Message
String subject = message.getSubject();

_log.info("Got an email. Subject = " + subject);

String contentType = message.getContentType();
_log.info("Email Content Type : " + contentType);



printParts(message);

//Parse out the Multiparts
//Perform business logic based on the email
}
catch (Exception ex) {
_log.log(Level.WARNING, "Failure in receiving email : " + ex.getMessage());
}
}

private static void printParts(Part p) throws IOException, MessagingException {
Object o = p.getContent();

if (o instanceof String) {
System.out.println("This is a String");
System.out.println((String)o);
}
else if (o instanceof Multipart) {
System.out.println("This is a Multipart");
Multipart mp = (Multipart)o;

int count = mp.getCount();
for (int i = 0; i < count; i++) {
printParts(mp.getBodyPart(i));
}
}
else if (o instanceof InputStream) {
System.out.println("This is just an input stream");
InputStream is = (InputStream)o;
int c;
while ((c = is.read()) != -1)
System.out.write(c);
}
}

private static void sendmail() {

_log.info("Email Sent");

Properties props2 = new Properties();
Session session2 = Session.getDefaultInstance(props2, null);

String msgBody = "...";

try {
Message msg = new MimeMessage(session2);
msg.setFrom(new InternetAddress("booya@cyclonecentral.appspotmail.com", "Example.com Admin"));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress("me@gmail.com", "Mr. User"));
msg.setSubject("Your Example.com account has been activated");
msg.setText(msgBody);
Transport.send(msg);

} catch (AddressException e) {
// ...
} catch (MessagingException e) {
// ...
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

不用说,它没有向我发送邮件,这让我想知道是否是因为存在一些冲突?

TIA

最佳答案

您绝对没有理由不能从应用程序中的任何位置(包括邮件处理程序)发送电子邮件。如果它不起作用,您应该检查请求日志以确定原因。

如果缩进正确,您的代码将更容易阅读很多

关于java - 发送和接收来自同一类(class)的邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8145881/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com