Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.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 发送和接收来自同一类的邮件?_Java_Google App Engine - Fatal编程技术网

Java 发送和接收来自同一类的邮件?

Java 发送和接收来自同一类的邮件?,java,google-app-engine,Java,Google App Engine,现在只是测试一下,但是你能像下面一样发送和接收来自同一类的邮件吗 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.Logge

现在只是测试一下,但是你能像下面一样发送和接收来自同一类的邮件吗

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();
    }
}

}
package com.mailserver;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.UnsupportedEncodingException;
导入java.util.Properties;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入javax.mail.MessaginException;
导入javax.mail.Multipart;
导入javax.mail.Part;
导入javax.mail.Session;
导入javax.mail.internet.mimessage;
导入javax.servlet.ServletException;
导入javax.servlet.http.*;
导入javax.mail.Message;
导入javax.mail.MessaginException;
导入javax.mail.Session;
导入javax.mail.Transport;
导入javax.mail.internet.AddressException;
导入javax.mail.internet.InternetAddress;
导入javax.mail.internet.mimessage;
@抑制警告(“串行”)
公共类MailHandlerServlet扩展了HttpServlet{
公共静态最终记录器_log=Logger.getLogger(MailHandlerServlet.class.getName());
@凌驾
public void doPost(HttpServletRequest-req、HttpServletResponse-resp)
抛出ServletException、IOException{
sendmail();
试一试{
Properties props=新属性();
Session Session=Session.getDefaultInstance(props,null);
MimeMessage message=新的MimeMessage(session,req.getInputStream());
//从Mime消息中提取重要字段
字符串subject=message.getSubject();
_log.info(“收到电子邮件。主题=”+主题);
字符串contentType=message.getContentType();
_log.info(“电子邮件内容类型:“+contentType”);
打印部件(消息);
//解析出多部分
//基于电子邮件执行业务逻辑
}
捕获(例外情况除外){
_log.log(Level.WARNING,“接收电子邮件失败:+ex.getMessage());
}
}
私有静态void打印部件(Part p)引发IOException、MessaginException{
对象o=p.getContent();
如果(字符串的o实例){
System.out.println(“这是一个字符串”);
System.out.println((字符串)o);
}
else if(多部分的o实例){
System.out.println(“这是一个多部分”);
多部分mp=(多部分)o;
int count=mp.getCount();
for(int i=0;i
不用说,这不是给我发邮件,这让我怀疑是不是因为有冲突


TIA

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

如果代码缩进正确,阅读起来会容易得多