public static boolean sendMessage(String email, String subject, String text) { if (email.isEmpty()) { logger.info("Email is not sent because the address is empty"); return false; } try { Gmail gmail = new Builder(httpTransport, JSON_FACTORY, credential).setApplicationName("XMage Server").build(); MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties())); mimeMessage.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(email)); mimeMessage.setSubject(subject); mimeMessage.setText(text); ByteArrayOutputStream baos = new ByteArrayOutputStream(); mimeMessage.writeTo(baos); Message message = new Message(); message.setRaw(Base64.encodeBase64URLSafeString(baos.toByteArray())); gmail.users().messages().send(ConfigSettings.instance.getGoogleAccount() + (ConfigSettings.instance.getGoogleAccount().endsWith("@gmail.com") ? "" : "@gmail.com"), message).execute(); return true; } catch (MessagingException | IOException ex) { logger.error("Error sending message", ex); } return false; } }
/** * Sends the specified message to the recipients in the To, Cc, and Bcc headers. * * Create a request for the method "messages.send". * * This request holds the parameters needed by the the gmail server. After setting any optional * parameters, call the {@link Send#execute()} method to invoke the remote operation. <p> {@link * Send#initialize(com.google.api.client.googleapis.services.AbstractGoogleClientRequest)} must be * called to initialize this instance immediately after invoking the constructor. </p> * * <p> * This constructor should be used for uploading media content. * </p> * * @param userId The user's email address. The special value me can be used to indicate the authenticated user. * [default: me] * @param content the {@link com.google.api.services.gmail.model.Message} media metadata or {@code null} if none * @param mediaContent The media HTTP content or {@code null} if none. * @since 1.13 */ protected Send(java.lang.String userId, com.google.api.services.gmail.model.Message content, com.google.api.client.http.AbstractInputStreamContent mediaContent) { super(Gmail.this, "POST", "/upload/" + getServicePath() + REST_PATH, content, com.google.api.services.gmail.model.Message.class); this.userId = com.google.api.client.util.Preconditions.checkNotNull(userId, "Required parameter userId must be specified."); initializeMediaUpload(mediaContent); }
/** * Sends the specified message to the recipients in the To, Cc, and Bcc headers. * * Create a request for the method "messages.send". * * This request holds the parameters needed by the the gmail server. After setting any optional * parameters, call the {@link Send#execute()} method to invoke the remote operation. <p> {@link * Send#initialize(com.google.api.client.googleapis.services.AbstractGoogleClientRequest)} must be * called to initialize this instance immediately after invoking the constructor. </p> * * @param userId The user's email address. The special value me can be used to indicate the authenticated user. * [default: me] * @param content the {@link com.google.api.services.gmail.model.Message} * @since 1.13 */ protected Send(java.lang.String userId, com.google.api.services.gmail.model.Message content) { super(Gmail.this, "POST", REST_PATH, content, com.google.api.services.gmail.model.Message.class); this.userId = com.google.api.client.util.Preconditions.checkNotNull(userId, "Required parameter userId must be specified."); checkRequiredParameter(content, "content"); checkRequiredParameter(content.getRaw(), "Message.getRaw()"); }
/** * Sends the specified message to the recipients in the To, Cc, and Bcc headers. * * Create a request for the method "messages.send". * * This request holds the parameters needed by the the gmail server. After setting any optional * parameters, call the {@link Send#execute()} method to invoke the remote operation. * * <p> * This method should be used for uploading media content. * </p> * * @param userId The user's email address. The special value me can be used to indicate the authenticated user. * [default: me] * @param content the {@link com.google.api.services.gmail.model.Message} media metadata or {@code null} if none * @param mediaContent The media HTTP content or {@code null} if none. * @return the request * @throws java.io.IOException if the initialization of the request fails */ public Send send(java.lang.String userId, com.google.api.services.gmail.model.Message content, com.google.api.client.http.AbstractInputStreamContent mediaContent) throws java.io.IOException { Send result = new Send(userId, content, mediaContent); initialize(result); return result; }
/** * Sends the specified message to the recipients in the To, Cc, and Bcc headers. * * Create a request for the method "messages.send". * * This request holds the parameters needed by the gmail server. After setting any optional * parameters, call the {@link Send#execute()} method to invoke the remote operation. * * @param userId The user's email address. The special value me can be used to indicate the authenticated user. * [default: me] * @param content the {@link com.google.api.services.gmail.model.Message} * @return the request */ public Send send(java.lang.String userId, com.google.api.services.gmail.model.Message content) throws java.io.IOException { Send result = new Send(userId, content); initialize(result); return result; }
public Message sendMessage(Gmail service, String to, String from, String subject, String bodyText, Document attachment) throws MessagingException, IOException { MimeMessage mimeMessage = createEmailWithAttachment(to, from, subject, bodyText, attachment); Message message = service.users().messages().send(from, createMessageWithEmail(mimeMessage)).execute(); return message; }
@Before public void setUp() { try { when(auth.getGmailService(anyString(), anyString())).thenReturn(gmailService); when(gmailService.users()).thenReturn(gmailUsers); when(gmailUsers.messages()).thenReturn(gmailUserMessages); when(gmailUserMessages.send(anyString(), anyObject())).thenReturn(gmailUserMessagesSend); when(gmailUserMessagesSend.execute()).thenReturn(new Message()); } catch (Exception e) { fail(e.getMessage()); } }