javax.mail.Transport类的使用及代码示例

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

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

Transport介绍

[英]An abstract class that models a message transport. Subclasses provide actual implementations.

Note that Transport extends the Service class, which provides many common methods for naming transports, connecting to transports, and listening to connection events.
[中]对消息传输进行建模的抽象类。子类提供了实际的实现。
请注意,Transport扩展了Service类,该类提供了许多用于命名传输、连接到传输和侦听连接事件的常用方法。

代码示例

代码示例来源:origin: pentaho/pentaho-kettle

Properties props = new Properties();
 props.put( "mail.smtp.host", ArgList[0] );
 Session session = Session.getDefaultInstance( props, null );
 session.setDebug( debug );
 Message msg = new MimeMessage( session );
 InternetAddress addressFrom = new InternetAddress( (String) ArgList[1] );
 msg.setFrom( addressFrom );
  addressTo[i] = new InternetAddress( strArrRecipients[i] );
 Transport.send( msg );
} catch ( Exception e ) {
 throw new RuntimeException( "sendMail: " + e.toString() );

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

String from = "user name";
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", "asdfgh");
props.put("mail.smtp.port", "587"); // 587 is the port number of yahoo mail
props.put("mail.smtp.auth", "true");
message.setFrom(new InternetAddress(from));
while (to_address[i] != null) {
  message.addRecipient(Message.RecipientType.TO, to_address[i]);
  i++;
message.setSubject("sending in a group");
message.setText("Welcome to JavaMail");
Transport transport = session.getTransport("smtp");
transport.connect("smtp.mail.yahoo.co.in", "user name", "asdfgh");
transport.sendMessage(message, message.getAllRecipients());
transport.close();

代码示例来源:origin: spring-projects/spring-framework

if (transport == null || !transport.isConnected()) {
    if (transport != null) {
      try {
        transport.close();
    if (mimeMessage.getSentDate() == null) {
      mimeMessage.setSentDate(new Date());
    String messageId = mimeMessage.getMessageID();
    mimeMessage.saveChanges();
    if (messageId != null) {
    Address[] addresses = mimeMessage.getAllRecipients();
    transport.sendMessage(mimeMessage, (addresses != null ? addresses : new Address[0]));
try {
  if (transport != null) {
    transport.close();

代码示例来源: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: networknt/light-4j

/**
 * Send email with a string content.
 *
 * @param to destination email address
 * @param subject email subject
 * @param content email content
 * @throws MessagingException message exception
 */
public void sendMail (String to, String subject, String content) throws MessagingException {
  Properties props = new Properties();
  props.put("mail.smtp.user", emailConfg.getUser());
  props.put("mail.smtp.host", emailConfg.getHost());
  props.put("mail.smtp.port", emailConfg.getPort());
  props.put("mail.smtp.starttls.enable","true");
  props.put("mail.smtp.debug", emailConfg.getDebug());
  props.put("mail.smtp.auth", emailConfg.getAuth());
  props.put("mail.smtp.ssl.trust", emailConfg.host);
  SMTPAuthenticator auth = new SMTPAuthenticator(emailConfg.getUser(), (String)secret.get(SecretConstants.EMAIL_PASSWORD));
  Session session = Session.getInstance(props, auth);
  MimeMessage message = new MimeMessage(session);
  message.setFrom(new InternetAddress(emailConfg.getUser()));
  message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
  message.setSubject(subject);
  message.setContent(content, "text/html");
  // Send message
  Transport.send(message);
  if(logger.isInfoEnabled()) logger.info("An email has been sent to " + to + " with subject " + subject);
}

代码示例来源:origin: pentaho/pentaho-kettle

data.props.put( "mail.smtps.quitwait", "false" );
data.props.put( "mail." + protocol + ".host", server );
Session session = Session.getInstance( data.props );
session.setDebug( isDebug() );
Message msg = new MimeMessage( session );
  email_address = senderName + '<' + email_address + '>';
  address[i] = new InternetAddress( reply_Address_List[i] );
 transport = session.getTransport( protocol );
 if ( meta.isUsingAuthentication() ) {
  if ( port != -1 ) {
   transport.connect( Const.NVL( server, "" ), port, Const.NVL( authenticationUser, "" ), Const.NVL(
    authenticationPassword, "" ) );
  } else {
   transport.connect( Const.NVL( server, "" ), Const.NVL( authenticationUser, "" ), Const.NVL(
    authenticationPassword, "" ) );
  transport.connect();
 transport.sendMessage( msg, msg.getAllRecipients() );
} finally {
 if ( transport != null ) {
  transport.close();

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

out.println("<h1>Sending email using JavaMail API</h1>");
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.transport.protocol", "smtp");
props.put("mail.debug", "true");
Session session = Session.getInstance(props,
  new javax.mail.Authenticator() {
    @Override
    + "\"...<br>");
  Message message = new MimeMessage(session);
  message.setFrom(new InternetAddress(creds.getFrom()));
  message.setRecipients(Message.RecipientType.TO,
    InternetAddress.parse(creds.getTo()));
  message.setSubject("Sending message using Programmatic JavaMail " + Calendar.getInstance().getTime());
  message.setText("Java EE 7 is cool!");
  Transport.send(message, message.getAllRecipients());

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

Properties properties = System.getProperties();
 properties.put("mail.smtp.host", server);
 properties.put("mail.smtp.port", "" + port);
 Session session = Session.getInstance(properties);
 Transport transport = session.getTransport("smtp");
 transport.connect(server, username, password);
 for (int i = 0; i < count; i++) {
   Message message = new MimeMessage(session);
   message.setFrom(new InternetAddress(from));
   InternetAddress[] address = {new InternetAddress(to)};
   message.setRecipients(Message.RecipientType.TO, address);
   message.setSubject(subject + "JavaMail API");
   message.setSentDate(new Date());
   setHTMLContent(message);
   message.saveChanges();
   transport.sendMessage(message, address);
 }
 transport.close();

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

String from = "example@live.com";
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.live.com");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
session.setDebug(true);
  message.setFrom(new InternetAddress(from));
  message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
  message.setSubject("Java");
  message.setText("Java");
  Transport trans = session.getTransport("smtp");
  trans.connect("smtp.live.com", 587, "example@live.com", "password");
  trans.sendMessage(message, message.getAllRecipients());

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

Properties props = new Properties();  
props.setProperty("mail.transport.protocol", "smtp");     
props.setProperty("mail.host", "smtp.gmail.com");  
props.put("mail.smtp.auth", "true");  
props.put("mail.smtp.port", "465");  
props.put("mail.debug", "true");  
props.put("mail.smtp.socketFactory.port", "465");  
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");  
Transport transport = session.getTransport();  
InternetAddress addressFrom = new InternetAddress(from);  
message.setSender(addressFrom);  
message.setSubject(subject);  
message.setContent(msg, "text/plain");  
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));  
transport.connect();  
Transport.send(message);  
transport.close();

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

String username = "mail@google.com";
 String password = "some-password";
 String recipient = "myemail@hotmail.com");
 Properties props = new Properties();
 props.put("mail.smtp.host", "smtp.gmail.com");
 props.put("mail.from", "myemail@gmail.com");
 props.put("mail.smtp.starttls.enable", "true");
 props.put("mail.smtp.port", "587");
 props.setProperty("mail.debug", "true");
 Session session = Session.getInstance(props, null);
 MimeMessage msg = new MimeMessage(session);
 msg.setRecipients(Message.RecipientType.TO, recipient);
 msg.setSubject("JavaMail hello world example");
 msg.setSentDate(new Date());
 msg.setText("Hello, world!\n");
 Transport transport = session.getTransport("smtp");
 transport.connect(username, password);
 transport.sendMessage(msg, msg.getAllRecipients());
 transport.close();

代码示例来源:origin: crashub/crash

public Boolean call() throws Exception {
 Properties props = new Properties();
 props.setProperty("mail.smtp.host", smtpHost);
 if (smtpPort != null) {
  props.setProperty("mail.smtp.port", Integer.toString(smtpPort));
 Session session = Session.getInstance(props, authenticator);
 MimeMessage message = new MimeMessage(session);
  message.setFrom(new InternetAddress(smtpFrom));
 message.setRecipients(Message.RecipientType.TO, addresses);
 if (subject != null) {
  message.setSubject(subject);
  Transport.send(message);

代码示例来源:origin: magefree/mage

properties.setProperty("mail.smtps.host", config.getMailSmtpHost());
properties.setProperty("mail.smtps.port", config.getMailSmtpPort());
properties.setProperty("mail.smtps.auth", "true");
properties.setProperty("mail.user", config.getMailUser());
properties.setProperty("mail.password", config.getMailPassword());
Session session = Session.getDefaultInstance(properties);
  MimeMessage message = new MimeMessage(session);
  message.setFrom(new InternetAddress(config.getMailFromAddress()));
  message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
  message.setSubject(subject);
  message.setText(text);
  Transport trnsport = session.getTransport("smtps");
  trnsport.connect(null, properties.getProperty("mail.password"));
  message.saveChanges();
  trnsport.sendMessage(message, message.getAllRecipients());
  trnsport.close();

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

properties = getProperties(new PropertiesProvider(user));
if (properties == null || !properties.containsKey("mail.smtp.host")) {
  properties = getProperties(new PropertiesProvider(Context.getConfig()));
if (!properties.containsKey("mail.smtp.host")) {
  LOGGER.warn("No SMTP configuration found");
  return;
Session session = Session.getInstance(properties);
MimeMessage message = new MimeMessage(session);
String from = properties.getProperty("mail.smtp.from");
if (from != null) {
  message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail()));
message.setSubject(subject);
message.setSentDate(new Date());
try (Transport transport = session.getTransport()) {
  Context.getStatisticsManager().registerMail();
  transport.connect(
      properties.getProperty("mail.smtp.host"),
      properties.getProperty("mail.smtp.username"),
      properties.getProperty("mail.smtp.password"));
  transport.sendMessage(message, message.getAllRecipients());

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

public void send() throws MessagingException, UserException {
  Properties props = new Properties();
  ServerSettings serverSettings = bimServer.getServerSettingsCache().getServerSettings();
  props.put("mail.smtp.localhost", "bimserver.org");
  String smtpProps = serverSettings.getSmtpProtocol() == SmtpProtocol.SMTPS ? "mail.smtps.port" : "mail.smtp.port";
  props.put("mail.smtp.connectiontimeout", 10000);
  props.put("mail.smtp.timeout", 10000);
  props.put("mail.smtp.writetimeout", 10000);
  Session mailSession = Session.getInstance(props);
      throw new RuntimeException("Unimplemented SMTP protocol: " + serverSettings.getSmtpProtocol());
    transport.connect(serverSettings.getSmtpServer(), serverSettings.getSmtpPort(), serverSettings.getSmtpUsername(), serverSettings.getSmtpPassword());
    Message message = new MimeMessage(mailSession);
    message.setSubject(subject);
    message.setRecipients(to, addressTo);
    message.setFrom(from);
    transport.sendMessage(message, addressTo);
  } catch (MessagingException e) {
    LOGGER.error("Error sending email " + body + " " + e.getMessage());

代码示例来源: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: apache/usergrid

String protocol = props.getProperty( PROP_TRANSPORT, "smtp" );
String host = props.getProperty( MAIL_PROP_PREFIX + protocol + ".host" );
String username = props.getProperty( MAIL_PROP_PREFIX + protocol + ".username" );
String password = props.getProperty( MAIL_PROP_PREFIX + protocol + ".password" );
Session session = Session.getInstance( props );
MimeMessage msg = new MimeMessage( session );
msg.setContent( msgContent );
msg.setFrom( new InternetAddress( from ) );
msg.addRecipient( Message.RecipientType.TO, new InternetAddress( to ) );
msg.setSubject( subject );
Transport transport = session.getTransport();
  transport.connect( host, null, null );
}else{
  transport.connect( host, username, password );
transport.sendMessage( msg, msg.getAllRecipients() );
transport.close();
logger.info( String.format( LOG_PREFIX_OK + " Message-id: "+msg.getMessageID(), to ) );

代码示例来源: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: stackoverflow.com

props.setProperty("mail.smtps.host", sender_host);
props.setProperty("mail.smtp.port", ssl_port);
props.setProperty("mail.smtp.startssl.enable", "true");
props.setProperty("mail.smtps.auth", "true");
props.put("mail.smtps.quitwait", "false");
  msg.setFrom(new InternetAddress(fromEmail));
  msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail, false));
  msg.setSubject("Test Mail");
  msg.setContent("Testing Mail", "text/html; charset=utf-8");
  msg.setSentDate(new Date());
Transport transport = session.getTransport("smtps");
transport.connect(sender_host, userName, password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
} catch (MessagingException e) {

相关文章