使用Java发送电子邮件

使用Java发送电子邮件,第1张

使用Java发送电子邮件

户名+密码不再是推荐的解决方案。这是因为

我尝试了此 *** 作,Gmail向该代码中用作用户名的电子邮件发送了一封电子邮件,其中说我们最近阻止了你的Google帐户的登录尝试,并将我定向到此支持页面:support.google.com/accounts/answer/6010255因此它看起来很有效,用于发送邮件的电子邮件帐户需要降低自身的安全性

Google已发布Gmail API- https://developers.google.com/gmail/api/?hl= zh- CN。我们应该使用oAuth2方法,而不是用户名+密码。

这是与Gmail API配合使用的代码段。

GoogleMail.javaimport com.google.api.client.util.base64;import com.google.api.services.gmail.Gmail;import com.google.api.services.gmail.model.Message;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.util.Properties;import javax.mail.MessagingException;import javax.mail.Session;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;public class GoogleMail {    private GoogleMail() {    }    private static MimeMessage createEmail(String to, String cc, String from, 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(from);        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);        email.setText(bodyText);        return email;    }    private static Message createMessageWithEmail(MimeMessage email) throws MessagingException, IOException {        ByteArrayOutputStream baos = new ByteArrayOutputStream();        email.writeTo(baos);        String enpredEmail = base64.enprebase64URLSafeString(baos.toByteArray());        Message message = new Message();        message.setRaw(enpredEmail);        return message;    }    public static void Send(Gmail service, String recipientEmail, String ccEmail, String fromEmail, String title, String message) throws IOException, MessagingException {        Message m = createMessageWithEmail(createEmail(recipientEmail, ccEmail, fromEmail, title, message));        service.users().messages().send("me", m).execute();    }}

要通过oAuth2构建授权的Gmail服务,请使用以下代码段。

实用工具

import com.google.api.client.auth.oauth2.Credential;import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;import com.google.api.client.http.HttpTransport;import com.google.api.client.json.gson.GsonFactory;import com.google.api.client.util.store.FileDataStoreFactory;import com.google.api.services.gmail.Gmail;import com.google.api.services.gmail.GmailScopes;import com.google.api.services.oauth2.Oauth2;import com.google.api.services.oauth2.model.Userinfoplus;import java.io.BufferedWriter;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.nio.file.Files;import java.nio.file.Paths;import java.security.GeneralSecurityException;import java.util.HashSet;import java.util.Set;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.yccheok.jstock.engine.Pair;public class Utils {        private static final GsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();        private static HttpTransport httpTransport;    private static final Log log = LogFactory.getLog(Utils.class);    static {        try { // initialize the transport httpTransport = GoogleNetHttpTransport.newTrustedTransport();        } catch (IOException ex) { log.error(null, ex);        } catch (GeneralSecurityException ex) { log.error(null, ex);        }    }    private static File getGmailDataDirectory() {        return new File(org.yccheok.jstock.gui.Utils.getUserDataDirectory() + "authentication" + File.separator + "gmail");    }        public static Userinfoplus getUserInfo(Credential credentials) throws IOException    {        Oauth2 userInfoService = new Oauth2.Builder(httpTransport, JSON_FACTORY, credentials).setApplicationName("JStock").build();        Userinfoplus userInfo  = userInfoService.userinfo().get().execute();        return userInfo;    }    public static String loadEmail(File dataStoreDirectory)  {        File file = new File(dataStoreDirectory, "email");        try { return new String(Files.readAllBytes(Paths.get(file.toURI())), "UTF-8");        } catch (IOException ex) { log.error(null, ex); return null;        }    }    public static boolean saveEmail(File dataStoreDirectory, String email) {        File file = new File(dataStoreDirectory, "email");        try { //If the constructor throws an exception, the finally block will NOT execute BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")); try {     writer.write(email); } finally {     writer.close(); } return true;        } catch (IOException ex){ log.error(null, ex); return false;        }    }    public static void logoutGmail() {        File credential = new File(getGmailDataDirectory(), "StoredCredential");        File email = new File(getGmailDataDirectory(), "email");        credential.delete();        email.delete();    }    public static Pair<Pair<Credential, String>, Boolean> authorizeGmail() throws Exception {        // Ask for only the permissions you need. Asking for more permissions will        // reduce the number of users who finish the process for giving you access        // to their accounts. It will also increase the amount of effort you will        // have to spend explaining to users what you are doing with their data.        // Here we are listing all of the available scopes. You should remove scopes        // that you are not actually using.        Set<String> scopes = new HashSet<>();        // We would like to display what email this credential associated to.        scopes.add("email");        scopes.add(GmailScopes.GMAIL_SEND);        // load client secrets        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(Utils.JSON_FACTORY, new InputStreamReader(Utils.class.getResourceAsStream("/assets/authentication/gmail/client_secrets.json")));        return authorize(clientSecrets, scopes, getGmailDataDirectory());    }        private static Pair<Pair<Credential, String>, Boolean> authorize(GoogleClientSecrets clientSecrets, Set<String> scopes, File dataStoreDirectory) throws Exception {        // Set up authorization pre flow.        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( httpTransport, JSON_FACTORY, clientSecrets, scopes) .setDataStoreFactory(new FileDataStoreFactory(dataStoreDirectory)) .build();        // authorize        return new MyAuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");    }    public static Gmail getGmail(Credential credential) {        Gmail service = new Gmail.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName("JStock").build();        return service; }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4985534.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-14
下一篇 2022-11-14

发表评论

登录后才能评论

评论列表(0条)

保存