Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.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
如何通过Google App Engine使用JavaMail发送电子邮件_Java_Google App Engine_Jakarta Mail_Sendmail_Gmail Api - Fatal编程技术网

如何通过Google App Engine使用JavaMail发送电子邮件

如何通过Google App Engine使用JavaMail发送电子邮件,java,google-app-engine,jakarta-mail,sendmail,gmail-api,Java,Google App Engine,Jakarta Mail,Sendmail,Gmail Api,我是应用引擎Google world的新手,但我有我的项目,要发送电子邮件,我使用的是JavaMail API,它运行良好,但我需要将“From”字段更改为不存在的帐户或与我的个人帐户不同(我不确定是否有必要在应用引擎中注册我需要在“From”中显示的帐户)字段)。我发送的电子邮件使用我在“发件人”字段中验证的帐户(很明显,不是吗)。所以问题是这是否可能?我也从这个网站上读了很多关于这个问题的网站,但我仍然没有工作。 GoogleAppEngine在API管理器中有Gmail API,但我不确定

我是应用引擎Google world的新手,但我有我的项目,要发送电子邮件,我使用的是JavaMail API,它运行良好,但我需要将“From”字段更改为不存在的帐户或与我的个人帐户不同(我不确定是否有必要在应用引擎中注册我需要在“From”中显示的帐户)字段)。我发送的电子邮件使用我在“发件人”字段中验证的帐户(很明显,不是吗)。所以问题是这是否可能?我也从这个网站上读了很多关于这个问题的网站,但我仍然没有工作。 GoogleAppEngine在API管理器中有Gmail API,但我不确定它是否与使用JavaMail API相同

我的一些代码发送电子邮件,但使用我的帐户的身份验证:

public void sendEmail(String[] recipients, String subject, String body, String username, String password) {

Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true"); //I tried disabling this but it not works
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
props.put("mail.smtp.port", "587"); //I tried with another port

//I tried without authentication from my account like this:
//Session.getDefaultInstance(props, null); 
//It not works
session = Session.getInstance(props, new Authenticator() {
     @Override
     protected PasswordAuthentication getPasswordAuthentication() {
     return new PasswordAuthentication(MailService.this.username, MailService.this.password);
    }
});

Message message = new MimeMessage(session);
// Here is the key, sending email not from authenticated account
message.setFrom(new InternetAddress("whateveraccount@example.com", "whateveraccount.engine@example.com")); 
message.setReplyTo(InternetAddress.parse("whateveraccount@example.com",false));

//Sending to multiple recipients
Address[] to = new Address[recipients.length];
for (int i=0; i<recipients.length; i++) {
    to[i] = new InternetAddress(recipients[i]);
}

message.setRecipients(Message.RecipientType.TO, to);
message.setSubject(subject);

/**
 Multi part message email
 **/

Multipart multipart = new MimeMultipart();

//body
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(body, "text/html");
multipart.addBodyPart(htmlPart);

// adds attachments
String[] attachFiles = new String[2];
attachFiles[0] = "..path to send attachment..";
attachFiles[1] = "..path to send attachment..";

if(attachFiles != null && attachFiles.length > 0){
    for (String filePath : attachFiles) {
        MimeBodyPart attachPart = new MimeBodyPart();
        try {
            attachPart.attachFile(filePath);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        multipart.addBodyPart(attachPart);
    }
}

message.setContent(multipart);
Transport.send(message);
}
public void sendmail(字符串[]收件人、字符串主题、字符串正文、字符串用户名、字符串密码){
Properties props=新属性();
props.put(“mail.smtp.auth”,“true”);
props.put(“mail.smtp.starttls.enable”,“true”);//我尝试禁用此功能,但无效
put(“mail.smtp.host”、“smtp.gmail.com”);
put(“mail.smtp.ssl.trust”、“smtp.gmail.com”);
props.put(“mail.smtp.port”,“587”);//我尝试了另一个端口
//我在没有从我的帐户进行身份验证的情况下进行了如下尝试:
//getDefaultInstance(props,null);
//它不起作用
session=session.getInstance(props,new Authenticator(){
@凌驾
受保护的密码身份验证getPasswordAuthentication(){
返回新的PasswordAuthentication(MailService.this.username,MailService.this.password);
}
});
Message Message=新的mimessage(会话);
//这是密钥,不是从经过身份验证的帐户发送电子邮件
message.setFrom(新的InternetAddress(“whateveraccount@example.com“什么算什么。engine@example.com")); 
message.setReplyTo(InternetAddress.parse(“whateveraccount@example.com“,假”);
//发送到多个收件人
地址[]收件人=新地址[recipients.length];
对于(int i=0;i 0){
for(字符串文件路径:attachFiles){
MimeBodyPart attachPart=新的MimeBodyPart();
试一试{
attachPart.attachFile(文件路径);
}捕获(IOEX异常){
例如printStackTrace();
}
多部件添加车身部件(附件部件);
}
}
message.setContent(多部分);
传输。发送(消息);
}
更新:
更具体地说,我需要在Google App Engine中进行配置。

您不能在
setFrom()调用中使用不存在的电子邮件地址

发件人:

  • 要设置邮件发件人和收件人,请使用InternetAddress类

    a。通过调用mimessage对象上的setFrom()方法来标识发送方。或者,您可以提供个人姓名作为 第二个参数中的字符串。有关哪封电子邮件的详细信息 可以用作发件人地址的地址,请参阅

  • 并详细说明了发件人电子邮件地址的限制:

    出于安全目的,邮件的发件人地址必须是以下地址之一: 以下是:

    • 当前登录用户的Gmail或Google Apps帐户
    • 任何形式的电子邮件地址:anything@[APP_NAME].appspotmail.com或anything@[APP_ALIAS].appspotmail.com
    • 云平台控制台下列出的任何电子邮件地址
    列表中的所有电子邮件地址都需要 要成为有效的Gmail或Google托管域帐户。应用程序管理员 可以将以下帐户添加到授权发件人列表中:

    • 他们自己的电子邮件地址
    • 作为所有者或管理者的任何组
    • 托管在Google Apps域中的应用程序:noreply@[domain].com,只要noreply@[domain].com是有效的帐户(用户或组)
    此外,由Google Apps管理的域的域管理员 可以将其域中的任何用户添加到授权发件人列表中


    通过使用Sendgrid,您可以从控制台中声明的域以外的域发送电子邮件

    你只需要做一些事情,比如:

        SendGrid sendgrid = new SendGrid(Constants.SENDGRID_API_KEY);
        SendGrid.Email email = new SendGrid.Email();
        email.addTo("recipient@gmail.com");
        email.setFrom("whatever@whatever.com");
        email.setFromName("Whatever");
        email.setSubject(...);
        ....
    
    文档非常好,从AppEngine邮件API切换到Sendgrid非常简单


    您是否查看了“谁可以发送邮件”链接()?是的,我知道了。你知道我可以在应用程序引擎配置中注册哪个地址或域吗?在那之后,哪些方法需要改变。例如:setFrom(新的InternetAddress(“anything@app_name.com”);如果单击文档中的“电子邮件API授权发件人”链接()