这是我通过gmail id发送邮件的示例代码。当我在我的eclipse tomcat上部署它时,我能够成功发送电子邮件。
但是当我在heroku上使用相同的代码并使用其余的Api传递必要的参数时,我会在我的日志中得到这个
请在浏览器中打开以下地址: Auth URL
当我点击此网址时,它要求我同意访问各种范围的Gmail,我点击允许,然后我收到错误
[Fiddler]与localhost的套接字连接失败。 ErrorCode:10061。 无法建立连接,因为目标计算机主动拒绝它127.0.0.1:41138
请让我知道我错过了什么。
我的代码在这里
public class GmailQuickstart {
private static final String APPLICATION_NAME =
"Gmail API Java Quickstart";
private static final java.io.File DATA_STORE_DIR = new java.io.File(
System.getProperty("user.home"), ".credentials/gmail-java-quickstart.json");
private static FileDataStoreFactory DATA_STORE_FACTORY;
private static final JsonFactory JSON_FACTORY =
JacksonFactory.getDefaultInstance();
private static HttpTransport HTTP_TRANSPORT;
private static final List<String> SCOPES =
Arrays.asList(GmailScopes.GMAIL_LABELS,GmailScopes.GMAIL_COMPOSE, GmailScopes.GMAIL_INSERT, GmailScopes.GMAIL_MODIFY, GmailScopes.GMAIL_READONLY, GmailScopes.MAIL_GOOGLE_COM);
private static final String HashMap = null;
static {
try {
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
} catch (Throwable t) {
t.printStackTrace();
System.exit(1);
}
}
public static Credential authorize() throws IOException {
// Load client secrets.
InputStream in =
GmailQuickstart.class.getResourceAsStream("/resources/client_secret.json");
GoogleClientSecrets clientSecrets =
GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
GoogleAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(DATA_STORE_FACTORY)
.setAccessType("offline")
.build();
Credential credential = new AuthorizationCodeInstalledApp(
flow, new LocalServerReceiver()).authorize("user");
System.out.println(
"Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
return credential;
}
public static Gmail getGmailService() throws IOException {
Credential credential = authorize();
return new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
}
private static MimeMessage createEmail(String to, String cc, String subject, String bodyText) throws MessagingException {
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
MimeMessage email = new MimeMessage(session);
InternetAddress tAddress = new InternetAddress(to);
InternetAddress cAddress = cc.isEmpty() ? null : new InternetAddress(cc);
InternetAddress fAddress = new InternetAddress("abc@gmail.com");
email.setFrom(fAddress);
if (cAddress != null) {
email.addRecipient(javax.mail.Message.RecipientType.CC, cAddress);
}
email.addRecipient(javax.mail.Message.RecipientType.TO, tAddress);
email.setSubject(subject);
Multipart mp = new MimeMultipart();
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setText(bodyText);
mp.addBodyPart(htmlPart);
email.setContent(mp);
return email;
}
private static Message createMessageWithEmail(MimeMessage email) throws MessagingException, IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
email.writeTo(baos);
String encodedEmail = Base64.encodeBase64URLSafeString(baos.toByteArray());
Message message = new Message();
message.setRaw(encodedEmail);
return message;
}
public static void sendMail(Map map) throws IOException, MessagingException {
Map<String, String> template=null;
if("welcome".equalsIgnoreCase((String)map.get("type"))){
template= EmailTemplateReader.readTemplate("welcome");
}else{
String status = (String)(((HashMap) map.get("data")).get("status"));
template = EmailTemplateReader.readTemplate(status.toLowerCase());
}
HashMap<String,String> dataMap = (HashMap<String,String>)map.get("data");
for (Map.Entry<String, String> entry : template.entrySet()) {
String value=entry.getValue();
for (Map.Entry<String,String> dataentry : dataMap.entrySet()) {
value = value.replace("#" + dataentry.getKey() + "#", dataentry.getValue());
}
entry.setValue(value);
}
Message m = createMessageWithEmail(createEmail((String)map.get("to"), (String)map.get("cc"),(String) template.get("subject"), (String) template.get("content")));
getGmailService().users().messages().send("me", m).execute();
}