本文整理汇总了Java中com.sun.mail.smtp.SMTPTransport.connect方法的典型用法代码示例。如果您正苦于以下问题:Java SMTPTransport.connect方法的具体用法?Java SMTPTransport.connect怎么用?Java SMTPTransport.connect使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.mail.smtp.SMTPTransport
的用法示例。
在下文中一共展示了SMTPTransport.connect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: send
import com.sun.mail.smtp.SMTPTransport; //导入方法依赖的package包/类
@Override
public void send(String senderAddress, String senderName, String recipient, String content) {
if (StringUtils.isBlank(senderAddress)) {
throw new FeatureNotAvailable("3PID Email identity: sender address is empty - " +
"You must set a value for notifications to work");
}
if (StringUtils.isBlank(content)) {
throw new InternalServerError("Notification content is empty");
}
try {
InternetAddress sender = new InternetAddress(senderAddress, senderName);
MimeMessage msg = new MimeMessage(session, IOUtils.toInputStream(content, StandardCharsets.UTF_8));
msg.setHeader("X-Mailer", "mxisd"); // FIXME set version
msg.setSentDate(new Date());
msg.setFrom(sender);
msg.setRecipients(Message.RecipientType.TO, recipient);
msg.saveChanges();
log.info("Sending invite to {} via SMTP using {}:{}", recipient, cfg.getHost(), cfg.getPort());
SMTPTransport transport = (SMTPTransport) session.getTransport("smtp");
transport.setStartTLS(cfg.getTls() > 0);
transport.setRequireStartTLS(cfg.getTls() > 1);
log.info("Connecting to {}:{}", cfg.getHost(), cfg.getPort());
transport.connect(cfg.getHost(), cfg.getPort(), cfg.getLogin(), cfg.getPassword());
try {
transport.sendMessage(msg, InternetAddress.parse(recipient));
log.info("Invite to {} was sent", recipient);
} finally {
transport.close();
}
} catch (UnsupportedEncodingException | MessagingException e) {
throw new RuntimeException("Unable to send e-mail invite to " + recipient, e);
}
}
示例2: connectToSmtp
import com.sun.mail.smtp.SMTPTransport; //导入方法依赖的package包/类
/**
* Connects and authenticates to an SMTP server with OAuth2. You must have
* called {@code initialize}.
*
* @param host Hostname of the smtp server, for example {@code
* smtp.googlemail.com}.
* @param port Port of the smtp server, for example 587.
* @param userEmail Email address of the user to authenticate, for example
* {@code [email protected]}.
* @param oauthToken The user's OAuth token.
* @param debug Whether to enable debug logging on the connection.
*
* @return An authenticated SMTPTransport that can be used for SMTP
* operations.
*/
public static SMTPTransportInfo connectToSmtp(String host, int port, String userEmail,
String oauthToken, boolean debug) throws Exception
{
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.starttls.required", "true");
props.put("mail.smtp.sasl.enable", "true");
props.put("mail.smtp.sasl.mechanisms", "XOAUTH2");
props.put(OAuth2SaslClientFactory.OAUTH_TOKEN_PROP, oauthToken);
Session session = Session.getInstance(props);
session.setDebug(debug);
final URLName unusedUrlName = null;
SMTPTransport transport = new SMTPTransport(session, unusedUrlName);
// If the password is non-null, SMTP tries to do AUTH LOGIN.
transport.connect(host, port, userEmail, EMPTY_PASSWORD);
SMTPTransportInfo transportInfo = new SMTPTransportInfo();
transportInfo.session = session;
transportInfo.smtpTransport = transport;
return transportInfo;
}
示例3: send
import com.sun.mail.smtp.SMTPTransport; //导入方法依赖的package包/类
public static void send(String to, String sub, String msg) {
Properties prop = System.getProperties();
prop.put("mail.smtps.host", host);
prop.put("mail.smtps.auth", "true");
Session session = Session.getInstance(prop);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(sub);
message.setText(msg);
SMTPTransport t = (SMTPTransport)session.getTransport("smtps");
t.connect(host, user, password);
t.sendMessage(message, message.getAllRecipients());
t.close();
System.out.println("Email sent to "+to);
} catch (MessagingException ex) {
Logger.getLogger(EMail.class.getName()).log(Level.SEVERE, null, ex);
}
}
示例4: connectToSmtp
import com.sun.mail.smtp.SMTPTransport; //导入方法依赖的package包/类
/**
* Connects and authenticates to an SMTP server with OAuth2. You must have
* called {@code initialize}.
*
* @param host Hostname of the smtp server, for example {@code
* smtp.googlemail.com}.
* @param port Port of the smtp server, for example 587.
* @param userEmail Email address of the user to authenticate, for example
* {@code [email protected]}.
* @param oauthToken The user's OAuth token.
* @param debug Whether to enable debug logging on the connection.
*
* @return An authenticated SMTPTransport that can be used for SMTP
* operations.
*/
public static SMTPTransport connectToSmtp(String host,
int port,
String userEmail,
String oauthToken,
boolean debug) throws Exception {
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.starttls.required", "true");
props.put("mail.smtp.sasl.enable", "true");
props.put("mail.smtp.sasl.mechanisms", "XOAUTH2");
props.put(OAuth2SaslClientFactory.OAUTH_TOKEN_PROP, oauthToken);
Session session = Session.getInstance(props);
session.setDebug(debug);
final URLName unusedUrlName = null;
SMTPTransport transport = new SMTPTransport(session, unusedUrlName);
// If the password is non-null, SMTP tries to do AUTH LOGIN.
final String emptyPassword = "";
transport.connect(host, port, userEmail, emptyPassword);
return transport;
}
示例5: connectToSmtp
import com.sun.mail.smtp.SMTPTransport; //导入方法依赖的package包/类
private SMTPTransport connectToSmtp(String host, int port, String userEmail,
String oauthToken, boolean debug) throws MessagingException {
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.starttls.required", "true");
props.put("mail.smtp.sasl.enable", "false");
props.put("mail.smtp.ssl.enable", true);
session = Session.getInstance(props);
session.setDebug(debug);
final URLName unusedUrlName = null;
SMTPTransport transport = new SMTPTransport(session, unusedUrlName);
// If the password is non-null, SMTP tries to do AUTH LOGIN.
final String emptyPassword = null;
transport.connect(host, port, userEmail, emptyPassword);
byte[] response = String.format("user=%s\1auth=Bearer %s\1\1", userEmail,
oauthToken).getBytes();
response = BASE64EncoderStream.encode(response);
transport.issueCommand("AUTH XOAUTH2 " + new String(response),
235);
return transport;
}
示例6: connectToSmtp
import com.sun.mail.smtp.SMTPTransport; //导入方法依赖的package包/类
public SMTPTransport connectToSmtp(String userEmail, String oauthToken, Session session) throws Exception {
final URLName unusedUrlName = null;
SMTPTransport transport = new SMTPTransport(session, unusedUrlName);
final String emptyPassword = null;
final String host = "smtp.gmail.com";
final int port = 587;
transport.connect(host, port, userEmail, emptyPassword);
byte[] response = String.format("user=%s\1auth=Bearer %s\1\1", userEmail, oauthToken).getBytes();
response = BASE64EncoderStream.encode(response);
transport.issueCommand("AUTH XOAUTH2 " + new String(response), 235);
return transport;
}
示例7: connectToSmtp
import com.sun.mail.smtp.SMTPTransport; //导入方法依赖的package包/类
public SMTPTransport connectToSmtp(String userEmail, String oauthToken,
Session session) throws Exception {
final URLName unusedUrlName = null;
SMTPTransport transport = new SMTPTransport(session, unusedUrlName);
final String emptyPassword = null;
final String host = "smtp.gmail.com";
final int port = 587;
transport.connect(host, port, userEmail, emptyPassword);
byte[] response = String.format("user=%s\1auth=Bearer %s\1\1",
userEmail, oauthToken).getBytes();
response = BASE64EncoderStream.encode(response);
transport.issueCommand("AUTH XOAUTH2 " + new String(response), 235);
return transport;
}
示例8: mailSenderEmpty
import com.sun.mail.smtp.SMTPTransport; //导入方法依赖的package包/类
@Test
public void mailSenderEmpty() throws IOException, MessagingException {
Socket smtpSocket;
String hostAddress = greenMail.getSmtp().getBindTo();
int port = greenMail.getSmtp().getPort();
Session smtpSession = greenMail.getSmtp().createSession();
URLName smtpURL = new URLName(hostAddress);
SMTPTransport smtpTransport = new SMTPTransport(smtpSession, smtpURL);
try {
smtpSocket = new Socket(hostAddress, port);
smtpTransport.connect(smtpSocket);
assertThat(smtpTransport.isConnected(),is(equalTo(true)));
smtpTransport.issueCommand("MAIL FROM: <>", -1);
assertThat("250 OK", equalToIgnoringWhiteSpace(smtpTransport.getLastServerResponse()));
} finally {
smtpTransport.close();
}
}
示例9: sendMail
import com.sun.mail.smtp.SMTPTransport; //导入方法依赖的package包/类
public static void sendMail(String to, String subject, String content) throws MessagingException
{
String server_email = TheConfig.getProperty(TheConfig.SYSTEMEMAIL_ADDRESS);
String server_pwd = TheConfig.getProperty(TheConfig.SYSTEMEMAIL_PASSWORD);
Properties props = System.getProperties();
props.put("mail.smtps.host",TheConfig.getProperty("mail.smtps.host"));
props.put("mail.smtps.auth","true");
Session session = Session.getInstance(props, null);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(server_email));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
msg.setSubject(subject);
msg.setText(content);
msg.setHeader("X-Mailer", "Sisob Data Extractor System");
msg.setSentDate(new Date());
SMTPTransport t =(SMTPTransport)session.getTransport("smtps");
t.connect(TheConfig.getProperty("mail.smtps.host"), server_email, server_pwd);
t.sendMessage(msg, msg.getAllRecipients());
LOG.info("Email Response To " + to + ": " + t.getLastServerResponse());
t.close();
}
示例10: connectToSmtp
import com.sun.mail.smtp.SMTPTransport; //导入方法依赖的package包/类
private SMTPTransport connectToSmtp(Session session, String host, int port, String userEmail,
String oauthToken) throws MessagingException {
final URLName unusedUrlName = null;
SMTPTransport transport = new SMTPTransport(session, unusedUrlName);
// If the password is non-null, SMTP tries to do AUTH LOGIN.
final String emptyPassword = null;
transport.connect(host, port, userEmail, emptyPassword);
byte[] response = String.format("user=%s\1auth=Bearer %s\1\1", userEmail,
oauthToken).getBytes();
response = BASE64EncoderStream.encode(response);
transport.issueCommand("AUTH XOAUTH2 " + new String(response),
235);
return transport;
}
示例11: send
import com.sun.mail.smtp.SMTPTransport; //导入方法依赖的package包/类
public static void send(final String username, final String password, String recipientEmail, String ccEmail, String title, String message, MailConsts mailConsts) throws AddressException, MessagingException {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties props = System.getProperties();
props.setProperty("mail.smtps.host", mailConsts.getHost());
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", String.valueOf(mailConsts.getPort()));
props.setProperty("mail.smtp.socketFactory.port", String.valueOf(mailConsts.getPort()));
props.setProperty("mail.smtps.auth", "true");
props.put("mail.smtps.quitwait", "false");
Session session = Session.getInstance(props, null);
final MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(username + "@" + mailConsts.getPostfix()));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail, false));
if (ccEmail.length() > 0) {
msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccEmail, false));
}
msg.setSubject(title);
msg.setText(message, "utf-8");
msg.setSentDate(new Date());
SMTPTransport t = (SMTPTransport) session.getTransport("smtps");
t.connect(mailConsts.getHost(), username, password);
t.sendMessage(msg, msg.getAllRecipients());
t.close();
}
示例12: senderConnect
import com.sun.mail.smtp.SMTPTransport; //导入方法依赖的package包/类
public void senderConnect() throws NoSuchProviderException, MessagingException {
senderSession = Session.getInstance(pManager.getSenderProps());
//senderSession.setDebugOut(pManager.get_Log_Stream());
transport = (SMTPTransport)(senderSession.getTransport("smtp"));
transport.connect(pManager.get_SENDER_User(), pManager.get_SENDER_Pass());
}
示例13: send
import com.sun.mail.smtp.SMTPTransport; //导入方法依赖的package包/类
private void send(MimeMessage msg) throws MessagingException {
msg.setHeader("X-Mailer", "matrix-appservice-email");
msg.setSentDate(new Date());
msg.setRecipients(Message.RecipientType.TO, getIdentity());
SMTPTransport transport = (SMTPTransport) session.getTransport("smtp");
transport.setStartTLS(cfg.getTls() > 0);
transport.setRequireStartTLS(cfg.getTls() > 1);
transport.connect(cfg.getHost(), cfg.getPort(), cfg.getLogin(), cfg.getPassword());
log.info("Sending email via SMTP using {}:{}", cfg.getHost(), cfg.getPort());
try {
transport.sendMessage(msg, InternetAddress.parse(getIdentity()));
} catch (MessagingException e) {
log.error("mmm", e);
} finally {
transport.close();
}
}
示例14: connectToSMTPServer
import com.sun.mail.smtp.SMTPTransport; //导入方法依赖的package包/类
private void connectToSMTPServer(SMTPTransport t) throws MessagingException {
if (_usesAuth) {
t.connect(_mailHost, _mailUser, _mailPassword);
} else {
t.connect();
}
}
示例15: connectToSmtp
import com.sun.mail.smtp.SMTPTransport; //导入方法依赖的package包/类
/**
* Connects and authenticates to an SMTP server with XOAUTH. You must have
* called {@code initialize}.
*
* @param host Hostname of the smtp server, for example {@code
* smtp.googlemail.com}.
* @param port Port of the smtp server, for example 587.
* @param userEmail Email address of the user to authenticate, for example
* {@code [email protected]}.
* @param oauthToken The user's OAuth token.
* @param oauthTokenSecret The user's OAuth token secret.
* @param consumer The application's OAuthConsumer. For testing, use
* {@code getAnonymousConsumer()}.
* @param debug Whether to enable debug logging on the connection.
*
* @return An authenticated SMTPTransport that can be used for SMTP
* operations.
*/
public static SMTPTransport connectToSmtp(String host,
int port,
String userEmail,
String oauthToken,
String oauthTokenSecret,
OAuthConsumer consumer,
boolean debug) throws Exception {
Properties props = new Properties();
props.put("mail.smtp.ehlo", "true");
props.put("mail.smtp.auth", "false");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.starttls.required", "true");
props.put("mail.smtp.sasl.enable", "false");
Session session = Session.getInstance(props);
session.setDebug(debug);
final URLName unusedUrlName = null;
SMTPTransport transport = new SMTPTransport(session, unusedUrlName);
// If the password is non-null, SMTP tries to do AUTH LOGIN.
final String emptyPassword = null;
transport.connect(host, port, userEmail, emptyPassword);
/*
* I couldn't get the SASL infrastructure to work with JavaMail 1.4.3;
* I don't think it was ready yet in that release. So we'll construct the
* AUTH command manually.
*/
XoauthSaslResponseBuilder builder = new XoauthSaslResponseBuilder();
byte[] saslResponse = builder.buildResponse(userEmail,
XoauthProtocol.SMTP,
oauthToken,
oauthTokenSecret,
consumer);
saslResponse = BASE64EncoderStream.encode(saslResponse);
transport.issueCommand("AUTH XOAUTH " + new String(saslResponse),
235);
return transport;
}