javax.mail.Transport.connect()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(9.9k)|赞(0)|评价(0)|浏览(165)

本文整理了Java中javax.mail.Transport.connect()方法的一些代码示例,展示了Transport.connect()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Transport.connect()方法的具体详情如下:
包路径:javax.mail.Transport
类名称:Transport
方法名:connect

Transport.connect介绍

暂无

代码示例

代码示例来源:origin: stackoverflow.com

...
 props.put("mail.smtp.starttls.enable", "true");
 Session session = Session.getDefaultInstance(props);
 Transport trans = session.getTransport("smtp");
 trans.connect("smtp.live.com", 25, "user", "pass");

代码示例来源:origin: stackoverflow.com

Transport transport = session.getTransport("smtps");
transport.connect (smtp_host, smtp_port, smtp_username, smtp_password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();

代码示例来源:origin: blynkkk/blynk-server

private void send(String to, String subj, String body, String contentType) throws Exception {
  MimeMessage message = new MimeMessage(session);
  message.setFrom(from);
  message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
  message.setSubject(subj, "UTF-8");
  message.setContent(body, contentType);
  try (Transport transport = session.getTransport()) {
    transport.connect(host, username, password);
    transport.sendMessage(message, message.getAllRecipients());
  }
  log.debug("Mail sent to {}. Subj: {}", to, subj);
  log.trace("Mail body: {}", body);
}

代码示例来源:origin: stackoverflow.com

message.setFrom(new InternetAddress(from));
Transport transport = session.getTransport("smtp");
transport.connect("smtp.mail.yahoo.co.in", "user name", "asdfgh");
transport.sendMessage(message, message.getAllRecipients());
transport.close();

代码示例来源:origin: loklak/loklak_server

/**
 * Check SMTP login credentials
 * @param hostname the host address
 * @param username the username/login
 * @param password the password
 * @param encryption encryption type (must be none, starttls or tls)
 * @param port the port number
 * @param disableCertificateChecking disable certificate checking (behind a ssl-proxy or when the server has a self signed certificate)
 * @throws MessagingException on error
 */
public static void checkConnection(@Nonnull String hostname, @Nonnull String username,
                  @Nonnull String password, @Nonnull String encryption,
                  int port, boolean disableCertificateChecking) throws MessagingException{
  Properties props = createProperties(hostname, port, encryption, disableCertificateChecking);
  Session session = Session.getInstance(props, null);
  Transport transport = session.getTransport("smtp");
  transport.connect(username, password);
  transport.close();
}

代码示例来源:origin: stackoverflow.com

Transport transport = session.getTransport("smtp");
String mfrom = "yourGmailUsernameWithout@"// example laabidiraissi 
transport.connect("smtp.gmail.com", mfrom, "thepassword");
transport.sendMessage(message, message.getAllRecipients());

代码示例来源:origin: stackoverflow.com

Transport transport;
try {
  transport = session.getTransport("smtp");
  transport.connect("smtp.gmail.com", username, password);
  transport.close();

  //Authentication success
} catch (AuthenticationException e) {
  System.out.println("Authentication Exception");
   //Authentication failed. Handle this here.
}

代码示例来源:origin: stackoverflow.com

message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
  toAddress[i] = new InternetAddress(to[i]);
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();

代码示例来源:origin: igniterealtime/Openfire

transport.connect(host, port, username, password);
for (MimeMessage message : messages) {
    transport.sendMessage(message,
      message.getRecipients(MimeMessage.RecipientType.TO));
if (transport != null) {
  try {
    transport.close();

代码示例来源:origin: blynkkk/blynk-server

@Override
public void sendHtmlWithAttachment(String to, String subj, String body, QrHolder[] attachments) throws Exception {
  MimeMessage message = new MimeMessage(session);
  message.setFrom(from);
  message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
  message.setSubject(subj, "UTF-8");
  Multipart multipart = new MimeMultipart();
  MimeBodyPart bodyMessagePart = new MimeBodyPart();
  bodyMessagePart.setContent(body, TEXT_HTML_CHARSET_UTF_8);
  multipart.addBodyPart(bodyMessagePart);
  for (QrHolder qrHolder : attachments) {
    MimeBodyPart attachmentsPart = new MimeBodyPart();
    attachmentsPart.setDataHandler(new DataHandler(new ByteArrayDataSource(qrHolder.data, "image/jpeg")));
    attachmentsPart.setFileName(qrHolder.makeQRFilename());
    multipart.addBodyPart(attachmentsPart);
  }
  message.setContent(multipart);
  try (Transport transport = session.getTransport()) {
    transport.connect(host, username, password);
    transport.sendMessage(message, message.getAllRecipients());
  }
  log.debug("Mail sent to {}. Subj: {}", to, subj);
  log.trace("Mail body: {}", body);
}

代码示例来源:origin: stagemonitor/stagemonitor

private Transport getTransport(Session session) throws MessagingException {
  Transport transport = session.getTransport(alertingPlugin.getSmtpProtocol());
  transport.connect(alertingPlugin.getSmtpHost(), alertingPlugin.getSmtpPort(), alertingPlugin.getSmtpUser(), alertingPlugin.getSmtpPassword());
  return transport;
}

代码示例来源:origin: stackoverflow.com

int port = 587;
String host = "smtp.gmail.com";
String user = "username@gmail.com";
String pwd = "email password";

try {
  Properties props = new Properties();
  // required for gmail 
  props.put("mail.smtp.starttls.enable","true");
  props.put("mail.smtp.auth", "true");
  // or use getDefaultInstance instance if desired...
  Session session = Session.getInstance(props, null);
  Transport transport = session.getTransport("smtp");
  transport.connect(host, port, user, pwd);
  transport.close();
  System.out.println("success");
 } 
 catch(AuthenticationFailedException e) {
    System.out.println("AuthenticationFailedException - for authentication failures");
    e.printStackTrace();
 }
 catch(MessagingException e) {
    System.out.println("for other failures");
    e.printStackTrace();
 }

代码示例来源:origin: opensourceBIM/BIMserver

throw new RuntimeException("Unimplemented SMTP protocol: " + serverSettings.getSmtpProtocol());
  transport.connect(serverSettings.getSmtpServer(), serverSettings.getSmtpPort(), serverSettings.getSmtpUsername(), serverSettings.getSmtpPassword());
  message.setFrom(from);
  transport.sendMessage(message, addressTo);
} catch (MessagingException e) {
  LOGGER.error("Error sending email " + body + " " + e.getMessage());

代码示例来源:origin: inspectIT/inspectIT

@Test
public void connected() throws MessagingException {
  mailSender.smtpEnabled = true;
  when(objectFactoryMock.getSmtpTransport()).thenReturn(transportMock);
  mailSender.init();
  ExternalServiceStatus result = mailSender.getServiceStatus();
  assertThat(result, is(ExternalServiceStatus.CONNECTED));
  verify(objectFactoryMock).getSmtpTransport();
  verify(transportMock).connect(any(String.class), any(Integer.class), any(String.class), any(String.class));
  verify(transportMock).close();
  verifyNoMoreInteractions(objectFactoryMock, transportMock);
}

代码示例来源:origin: javaee-samples/javaee7-samples

message.setFrom(new InternetAddress(creds.getFrom()));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(creds.getTo()));
message.setSubject("Sending message using Annotated JavaMail "
message.setText("Java EE 7 is cool!");
Transport t = session.getTransport();
t.connect(creds.getFrom(), creds.getPassword());
t.sendMessage(message, message.getAllRecipients());

代码示例来源:origin: gocd/gocd

public ValidationBean send(String subject, String body, String to) {
  Transport transport = null;
  try {
    LOGGER.debug("Sending email [{}] to [{}]", subject, to);
    Properties props = mailProperties();
    MailSession session = MailSession.getInstance().createWith(props, username, password);
    transport = session.getTransport();
    transport.connect(host, port, nullIfEmpty(username), nullIfEmpty(password));
    MimeMessage msg = session.createMessage(from, to, subject, body);
    transport.sendMessage(msg, msg.getRecipients(TO));
    return ValidationBean.valid();
  } catch (Exception e) {
    LOGGER.error("Sending failed for email [{}] to [{}]", subject, to, e);
    return ValidationBean.notValid(ERROR_MESSAGE);
  } finally {
    if (transport != null) {
      try {
        transport.close();
      } catch (MessagingException e) {
        LOGGER.error("Failed to close transport", e);
      }
    }
  }
}

代码示例来源:origin: stackoverflow.com

Session session = Session.getDefaultInstance(props,new javax.mail.Authenticator() {  
    protected PasswordAuthentication getPasswordAuthentication() {  
      return new PasswordAuthentication("FROM","PASSWORD");  
    }  
  });
Transport transport = session.getTransport("smtp");
transport.connect();
try{
  for(Message m : messages) {
  transport.sendMessage(m, m.getAllRecipients()); // time decreased to 2 second/message
  }
}finally {
t.close();
}

代码示例来源:origin: org.seamless/seamless-util

protected Transport createConnectedTransport(Session session) throws MessagingException {
    Transport transport = session.getTransport("smtp");
    if (user != null && password != null)
      transport.connect(user, password);
    else
      transport.connect();
    return transport;
  }
}

代码示例来源:origin: FenixEdu/fenixedu-academic

@Override
  protected Result check() throws Exception {
    final Properties properties = new Properties();
    properties.put("mail.transport.protocol", "smtp");
    Transport transport = Session.getInstance(properties).getTransport();
    transport.connect(FenixEduAcademicConfiguration.getConfiguration().getMailSmtpHost(), null, null);
    String response = ((SMTPTransport) transport).getLastServerResponse();
    transport.close();
    return Result.healthy("SMTP server returned response: " + response);
  }
});

代码示例来源:origin: stackoverflow.com

Transport transport = session.getTransport("smtp");
   transport.connect(this._properties.getProperty("mail.smtp.host"), 
       Integer.parseInt(this._properties.getProperty("mail.smtp.port")),
       this._properties.getProperty("mail.smtp.user"),
       this._properties.getProperty("mail.smtp.password"));            
   Address[] addr = new Address[this._addresses.size()];
   for (int i = 0, c = this._addresses.size(); i < c; i++)
   {
     addr[i] = new InternetAddress(this._addresses.get(i));
   }
   transport.sendMessage(message, addr);

相关文章