本文整理匯總了Java中javax.mail.Transport.connect方法的典型用法代碼示例。如果您正苦於以下問題:Java Transport.connect方法的具體用法?Java Transport.connect怎麽用?Java Transport.connect使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.mail.Transport
的用法示例。
在下文中一共展示了Transport.connect方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: iMessage
import javax.mail.Transport; //導入方法依賴的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: sendCodeMail
import javax.mail.Transport; //導入方法依賴的package包/類
/**
* 發送郵件,從公郵裏發郵件給成員變量email。
*
* @throws Exception 可能會有異常拋出,建議打出Log。
*/
public void sendCodeMail() throws MessagingException, UnsupportedEncodingException {
if (email == null) {
Log.e("sendCodeMail","調用錯誤");
return;
}
//創建一封郵件
MimeMessage message = createCodeMessage(session, myEmailAccount, email);
Transport transport = session.getTransport();
//使用 郵箱賬號 和 密碼 連接郵件服務器, 這裏認證的郵箱必須與 message 中的發件人郵箱一致, 否則報錯
transport.connect(myEmailAccount, myEmailPassword);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
示例3: sendMail
import javax.mail.Transport; //導入方法依賴的package包/類
private static void sendMail()
throws MessagingException, IOException {
Session session = Session.getInstance(getMailProps(), new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
getVal("username"),
getVal("password"));
}
});
session.setDebug(getBoolVal("mail.debug"));
LOG.info("Compiling Mail before Sending");
Message message = createMessage(session);
Transport transport = session.getTransport("smtp");
LOG.info("Connecting to Mail Server");
transport.connect();
LOG.info("Sending Mail");
transport.sendMessage(message, message.getAllRecipients());
transport.close();
LOG.info("Reports are sent to Mail");
clearTempZips();
}
示例4: authentication
import javax.mail.Transport; //導入方法依賴的package包/類
/**
* authentication
*/
public static Boolean authentication(EmailSettingContent emailSetting) {
Properties props = buildProperty(emailSetting);
Session session = Session.getInstance(props, null);
try {
Transport transport = session.getTransport("smtp");
String username = null;
String password = null;
if (emailSetting.isAuthenticated()) {
username = emailSetting.getUsername();
password = emailSetting.getPassword();
}
transport.connect(emailSetting.getSmtpUrl(), emailSetting.getSmtpPort(), username,
password);
transport.close();
return true;
} catch (Throwable throwable) {
return false;
}
}
示例5: sendFeedBackMail
import javax.mail.Transport; //導入方法依賴的package包/類
/**
* 發送反饋郵件
* @param detail
* @throws UnsupportedEncodingException
* @throws MessagingException
*/
public void sendFeedBackMail(String detail) throws UnsupportedEncodingException, MessagingException {
MimeMessage message = createFeedBackMsg(session,myEmailAccount,detail);
Transport transport = session.getTransport();
transport.connect(myEmailAccount, myEmailPassword);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
示例6: connect
import javax.mail.Transport; //導入方法依賴的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;
}
示例7: sendout
import javax.mail.Transport; //導入方法依賴的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;
}
}
示例8: makeObject
import javax.mail.Transport; //導入方法依賴的package包/類
/**
* Create a new {@link Transport} using the {@link Session} returned by {@link JavaMailSenderImpl#getSession() getSession()}.
*
* @param key A {@link URLName} containing the connection details
* @return A new {@link Transport}
*/
@Override
public Object makeObject(Object key) throws Exception
{
if ((key instanceof URLName) == false)
{
throw new IllegalArgumentException("Invlid key type");
}
log.debug("Creating new Transport");
URLName urlName = (URLName) key;
Transport transport = getSession().getTransport(urlName.getProtocol());
transport.connect(urlName.getHost(), urlName.getPort(), urlName.getUsername(), urlName.getPassword());
return transport;
}
示例9: sendTokenMail
import javax.mail.Transport; //導入方法依賴的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;
}
}
示例10: authenticateAndLoadConfiguration
import javax.mail.Transport; //導入方法依賴的package包/類
/** Test smtp authentication configuration.
*
* @param smtpConfiguration
* @throws MessagingException
* @throws ConfigurationException
*/
public void authenticateAndLoadConfiguration(EmailConfigurationDTO smtpConfiguration) throws MessagingException, ConfigurationException {
Transport transport = getSession().getTransport(SMTP_PROTOCOL);
try {
transport.connect(smtpConfiguration.getHost(), smtpConfiguration.getPort(), smtpConfiguration.getUserName(),
smtpConfiguration.getPassword());
} finally {
transport.close();
}
updateMailSenderConfig();
}
示例11: send
import javax.mail.Transport; //導入方法依賴的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: sendSetupMail
import javax.mail.Transport; //導入方法依賴的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;
}
}
示例13: send
import javax.mail.Transport; //導入方法依賴的package包/類
/**
* @Title: send
* @Description: TODO(發送帶附件的郵件)
* @author [email protected] (苟誌強)
* @date 2017-6-6 下午4:29:56
* @param to 接收用戶賬戶
* @param title 郵件標題
* @param content 郵件內容
* @param affix 附件路徑
* @param affixName 發送後顯示名稱
* @return 是否發送成功
*/
public static boolean send(String to,String title,String content,String affix,String affixName) {
Properties props = new Properties();
//設置發送郵件的郵件服務器的屬性(這裏使用網易的smtp服務器)
props.put("mail.smtp.host", host);
//需要經過授權,也就是有戶名和密碼的校驗,這樣才能通過驗證
props.put("mail.smtp.auth", "true");
//用剛剛設置好的props對象構建一個session
Session session = Session.getDefaultInstance(props);
//有了這句便可以在發送郵件的過程中在console處顯示過程信息,供調試使
//用(你可以在控製台(console)上看到發送郵件的過程)
session.setDebug(true);
//用session為參數定義消息對象
MimeMessage message = new MimeMessage(session);
try{
//加載發件人地址
message.setFrom(new InternetAddress(from));
//加載收件人地址
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
//加載標題
message.setSubject(title);
// 向multipart對象中添加郵件的各個部分內容,包括文本內容和附件
Multipart multipart = new MimeMultipart();
//設置郵件的文本內容
BodyPart contentPart = new MimeBodyPart();
contentPart.setText(content);
multipart.addBodyPart(contentPart);
//添加附件
if(affix!=null&&!"".equals(affix)&&affixName!=null&&!"".equals(affixName)){
BodyPart messageBodyPart= new MimeBodyPart();
DataSource source = new FileDataSource(affix);
//添加附件的內容
messageBodyPart.setDataHandler(new DataHandler(source));
//添加附件的標題
messageBodyPart.setFileName(MimeUtility.encodeText(affixName));
multipart.addBodyPart(messageBodyPart);
}
//將multipart對象放到message中
message.setContent(multipart);
//保存郵件
message.saveChanges();
// 發送郵件
Transport transport = session.getTransport("smtp");
//連接服務器的郵箱
transport.connect(host, user, pwd);
//把郵件發送出去
transport.sendMessage(message, message.getAllRecipients());
transport.close();
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
示例14: sendout
import javax.mail.Transport; //導入方法依賴的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;
}
}
示例15: test
import javax.mail.Transport; //導入方法依賴的package包/類
public void test() throws Exception {
Properties props = System.getProperties();
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtp.port", PORT);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.starttls.required", "true");
Session session = Session.getInstance(props);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(FROM));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(TO));
msg.setSubject(SUBJECT);
msg.setContent(BODY, "text/plain");
Transport transport = session.getTransport();
try {
transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD);
transport.sendMessage(msg, msg.getAllRecipients());
} finally {
transport.close();
}
}