本文整理汇总了Java中javax.mail.Session.getTransport方法的典型用法代码示例。如果您正苦于以下问题:Java Session.getTransport方法的具体用法?Java Session.getTransport怎么用?Java Session.getTransport使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.mail.Session
的用法示例。
在下文中一共展示了Session.getTransport方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: iMessage
import javax.mail.Session; //导入方法依赖的package包/类
public static boolean iMessage() {
try {
message.setContent(mp);
message.saveChanges();
Session mailSession = Session.getInstance(props, null);
Transport transport = mailSession.getTransport("smtp");
transport.connect((String) props.get("mail.smtp.host"),
(Integer) props.get("mail.smtp.port"), username, password);
transport.sendMessage(message,
message.getRecipients(javax.mail.Message.RecipientType.TO));
transport.close();
} catch (MessagingException e) {
return false;
}
return true;
}
示例2: sendToAdmin
import javax.mail.Session; //导入方法依赖的package包/类
@Override
public boolean sendToAdmin(final String institutionEmailAddress, final String applicationMessage) {
String[] to = new String[]{"[email protected]"};
setupProperties();
Session session = Session.getDefaultInstance(properties);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(username));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for (int i = 0; i < to.length; i++) {
toAddress[i] = new InternetAddress(to[i]);
}
for (int i = 0; i < toAddress.length; i++) {
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject(APPLICATION_SUBJECT);
message.setText(applicationBody + "Email: " + institutionEmailAddress + "\nMessage: " + applicationMessage);
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
LOGGER.info("Email sent to Admin");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
示例3: sendout
import javax.mail.Session; //导入方法依赖的package包/类
/**
* 发送邮件
*/
public boolean sendout() {
try {
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
logger.info(Resources.getMessage("EMAIL.SENDING"));
Session mailSession = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
if (userkey == null || "".equals(userkey.trim())) {
return null;
}
return new PasswordAuthentication(username, userkey);
}
});
Transport transport = mailSession.getTransport("smtp");
transport.connect((String) props.get("mail.smtp.host"), username, password);
// 设置发送日期
mimeMsg.setSentDate(new Date());
// 发送
transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.TO));
if (mimeMsg.getRecipients(Message.RecipientType.CC) != null) {
transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.CC));
}
logger.info(Resources.getMessage("EMAIL.SEND_SUCC"));
transport.close();
return true;
} catch (Exception e) {
logger.error(Resources.getMessage("EMAIL.SEND_ERR"), e);
return false;
}
}
示例4: sendout
import javax.mail.Session; //导入方法依赖的package包/类
/**
* 发送邮件
*
* @param name String
* @param pass String
*/
public boolean sendout() {
try {
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
logger.info(Resources.getMessage("EMAIL.SENDING"));
Session mailSession = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
if (userkey == null || "".equals(userkey.trim())) {
return null;
}
return new PasswordAuthentication(username, userkey);
}
});
Transport transport = mailSession.getTransport("smtp");
transport.connect((String) props.get("mail.smtp.host"), username, password);
// 设置发送日期
mimeMsg.setSentDate(new Date());
// 发送
transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.TO));
if (mimeMsg.getRecipients(Message.RecipientType.CC) != null) {
transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.CC));
}
logger.info(Resources.getMessage("EMAIL.SEND_SUCC"));
transport.close();
return true;
} catch (Exception e) {
logger.error(Resources.getMessage("EMAIL.SEND_ERR"), e);
return false;
}
}
示例5: sendTokenMail
import javax.mail.Session; //导入方法依赖的package包/类
@Override
public boolean sendTokenMail(final String[] to, final String contractAddress, final String artifactId,
final String registryContract) {
setupProperties();
Session session = Session.getDefaultInstance(properties);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(username));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for (int i = 0; i < to.length; i++) {
toAddress[i] = new InternetAddress(to[i]);
}
for (int i = 0; i < toAddress.length; i++) {
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject(TOKEN_SUBJECT);
message.setText(tokenBody + contractAddress + "\nArtifact ID: " + artifactId
+ "\n\nBelow is the Unilog Registry. Add this into the verifiers portal if you need to"
+ " verify a token: \n" + registryContract);
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
LOGGER.info("Token email has been sent");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
示例6: sendSetupMail
import javax.mail.Session; //导入方法依赖的package包/类
@Override
public boolean sendSetupMail(final String[] to, final String code, final String subject, final String body) {
setupProperties();
Session session = Session.getDefaultInstance(properties);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(username));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for (int i = 0; i < to.length; i++) {
toAddress[i] = new InternetAddress(to[i]);
}
for (int i = 0; i < toAddress.length; i++) {
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
if (subject == null || body == null) {
message.setSubject(SETUP_SUBJECT);
message.setText(setupBody + code);
} else {
message.setSubject(subject);
message.setText(body + code);
}
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
LOGGER.info("Setup email has been sent.");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
示例7: getTransport
import javax.mail.Session; //导入方法依赖的package包/类
/**
* Obtain a Transport object from the given JavaMail Session,
* using the configured protocol.
* <p>Can be overridden in subclasses, e.g. to return a mock Transport object.
* @see javax.mail.Session#getTransport(String)
* @see #getProtocol()
*/
protected Transport getTransport(Session session) throws NoSuchProviderException {
String protocol = getProtocol();
if (protocol == null) {
protocol = session.getProperty("mail.transport.protocol");
if (protocol == null) {
protocol = DEFAULT_PROTOCOL;
}
}
return session.getTransport(protocol);
}
示例8: sendAlertEmail
import javax.mail.Session; //导入方法依赖的package包/类
public void sendAlertEmail(String alert) throws MessagingException{
String host = "smtp.gmail.com";
String from = "[email protected]";
String pass = "3inst3in?";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true"); // added this line
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[alert_recipients.length];
// To get the array of addresses
for( int i=0; i < alert_recipients.length; i++ ) { // changed from a while loop
toAddress[i] = new InternetAddress(alert_recipients[i]);
}
//System.out.println(Message.RecipientType.TO);
for( int i=0; i < toAddress.length; i++) { // changed from a while loop
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject("WARNING Something went wrong in the Einstein tunnel");
message.setText(alert);
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
示例9: connect
import javax.mail.Session; //导入方法依赖的package包/类
public static Boolean connect(Properties props)
throws MessagingException, IOException {
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
props.getProperty("username"),
props.getProperty("password"));
}
});
Transport transport = session.getTransport("smtp");
transport.connect();
transport.close();
return true;
}
示例10: sendAccountCreatedMail
import javax.mail.Session; //导入方法依赖的package包/类
@Override
public boolean sendAccountCreatedMail(final String[] to, final String email) {
setupProperties();
Session session = Session.getDefaultInstance(properties);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(username));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for (int i = 0; i < to.length; i++) {
toAddress[i] = new InternetAddress(to[i]);
}
for (int i = 0; i < toAddress.length; i++) {
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject(DETAILS_SUBJECT);
message.setText(detailsBody + "username: " + email);
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
LOGGER.info("Login details email has been sent.");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
示例11: send
import javax.mail.Session; //导入方法依赖的package包/类
/**
* 发送邮件
*
* @param email-收件人
* @throws Exception
* @return new password
*/
public static String send(String email, String username) throws Exception {
String pwd = getRandomString(6);
// 2. 根据配置创建会话对象, 用于和邮件服务器交互
Session session = Session.getDefaultInstance(props);
session.setDebug(true); // 设置为debug模式, 可以查看详细的发送 log
// 3. 创建一封邮件
MimeMessage message = createMimeMessage(session, myEmailAccount, email, username,pwd);
// 4. 根据 Session 获取邮件传输对象
Transport transport = session.getTransport();
// 5. 使用 邮箱账号 和 密码 连接邮件服务器, 这里认证的邮箱必须与 message 中的发件人邮箱一致, 否则报错
//
// PS_01: 成败的判断关键在此一句, 如果连接服务器失败, 都会在控制台输出相应失败原因的 log,
// 仔细查看失败原因, 有些邮箱服务器会返回错误码或查看错误类型的链接, 根据给出的错误
// 类型到对应邮件服务器的帮助网站上查看具体失败原因。
//
// PS_02: 连接失败的原因通常为以下几点, 仔细检查代码:
// (1) 邮箱没有开启 SMTP 服务;
// (2) 邮箱密码错误, 例如某些邮箱开启了独立密码;
// (3) 邮箱服务器要求必须要使用 SSL 安全连接;
// (4) 请求过于频繁或其他原因, 被邮件服务器拒绝服务;
// (5) 如果以上几点都确定无误, 到邮件服务器网站查找帮助。
//
// PS_03: 仔细看log, 认真看log, 看懂log, 错误原因都在log已说明。
transport.connect(myEmailAccount, myEmailPassword);
// 6. 发送邮件, 发到所有的收件地址, message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人,
// 抄送人, 密送人
transport.sendMessage(message, message.getAllRecipients());
// 7. 关闭连接
transport.close();
return pwd;
}
示例12: sendMail
import javax.mail.Session; //导入方法依赖的package包/类
/**
* Send mail.
*
* @param from
* Sender's email ID needs to be mentioned
* @param to
* Recipient's email ID needs to be mentioned.
* @param subject
* the subject
* @throws MessagingException
*/
public void sendMail(String from, String to, String subject, String body) throws MessagingException {
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", host);
properties.setProperty("mail.smtp.port", Integer.toString(port));
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
Transport transport = null;
try {
transport = session.getTransport();
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set Subject: header field
message.setSubject(subject);
// Now set the actual message
message.setText(body);
// Send message
transport.send(message);
System.out.println("Sent message successfully....");
} finally {
if (transport != null) {
transport.close();
}
}
}
示例13: sendPassword
import javax.mail.Session; //导入方法依赖的package包/类
public static void sendPassword(String receiveMailAccount, String password) throws Exception {
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", myEmailSMTPHost);
props.setProperty("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props);
session.setDebug(true);
MimeMessage message = createMimeMessage(session, myEmailAccount, receiveMailAccount, password);
Transport transport = session.getTransport();
transport.connect(myEmailAccount, myEmailPassword);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
示例14: sendMail
import javax.mail.Session; //导入方法依赖的package包/类
public boolean sendMail(String to,String otp) throws MessagingException
{
String host="smtp.gmail.com";
String username="";//emailid
String password="";//emailid password
String from=" ";// email from which u have to send
String subject="One Time Password";
String body="Your One Time passsword is "+otp;
boolean sessionDebug=false;
Properties props=System.getProperties();
props.put("mail.host",host);
props.put("mail.transport.protocol","smtp");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "25");
Session mailSession=Session.getDefaultInstance(props,null);
mailSession.setDebug(sessionDebug);
Message msg=new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress [] address={new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO,address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(body);
Transport tr=mailSession.getTransport("smtp");
tr.connect(host,username,password);
msg.saveChanges();
tr.sendMessage(msg,msg.getAllRecipients());
tr.close();
//Transport.send(msg);
return true;
}
示例15: sendMail1
import javax.mail.Session; //导入方法依赖的package包/类
public boolean sendMail1(String to,String passwrd, String link) throws MessagingException
{
String host="smtp.gmail.com";
String username=" ";//emailid
String password="";//emailid password
String from=" ";// email from which u have to send
String subject="Website Password";
String body="Your website password is "+passwrd+" Please click to reset "+link;
boolean sessionDebug=false;
Properties props=System.getProperties();
props.put("mail.host",host);
props.put("mail.transport.protocol","smtp");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "25");
Session mailSession=Session.getDefaultInstance(props,null);
mailSession.setDebug(sessionDebug);
Message msg=new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress [] address={new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO,address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(body);
Transport tr=mailSession.getTransport("smtp");
tr.connect(host,username,password);
msg.saveChanges();
tr.sendMessage(msg,msg.getAllRecipients());
tr.close();
//Transport.send(msg);
return true;
}