本文整理匯總了Java中javax.mail.Authenticator類的典型用法代碼示例。如果您正苦於以下問題:Java Authenticator類的具體用法?Java Authenticator怎麽用?Java Authenticator使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Authenticator類屬於javax.mail包,在下文中一共展示了Authenticator類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: sendMsg
import javax.mail.Authenticator; //導入依賴的package包/類
public boolean sendMsg(String recipient, String subject, String content)
throws MessagingException {
// Create a mail object
Session session = Session.getInstance(props, new Authenticator() {
// Set the account information session,transport will send mail
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(Constants.MAIL_USERNAME, Constants.MAIL_PASSWORD);
}
});
session.setDebug(true);
Message msg = new MimeMessage(session);
try {
msg.setSubject(subject); //Set the mail subject
msg.setContent(content,"text/html;charset=utf-8");
msg.setFrom(new InternetAddress(Constants.MAIL_USERNAME)); //Set the sender
msg.setRecipient(RecipientType.TO, new InternetAddress(recipient)); //Set the recipient
Transport.send(msg);
return true;
} catch (Exception ex) {
ex.printStackTrace();
System.out.println(ex.getMessage());
return false;
}
}
示例2: createSession
import javax.mail.Authenticator; //導入依賴的package包/類
/**
* Creates a mail session from the underlying mail properties.
*
* @return the mail session.
*/
public Session createSession()
{
Session session;
if (username == null)
{
session = Session.getInstance(properties);
}
else
{
Authenticator auth = new Authenticator()
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username,
password == null ? "" : password);
}
};
session = Session.getInstance(properties, auth);
}
return session;
}
示例3: init
import javax.mail.Authenticator; //導入依賴的package包/類
/**
* 郵件監聽器
*/
//private List<EmailSendListener> emailSendListeners = new ArrayList<EmailSendListener>();
public void init() {
final Properties properties = SysConfig.getProperties();
this.theadPool = Executors.newFixedThreadPool(POOL_SIZE);
this.session = Session.getDefaultInstance(properties,
new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(properties
.getProperty("mail.smtp.username"), properties
.getProperty("mail.smtp.password"));
}
});
username = properties.getProperty("mail.smtp.username");
//設置調試模式(輸出調試信息)
this.session.setDebug(false);
}
示例4: getSession
import javax.mail.Authenticator; //導入依賴的package包/類
/**
* 獲取用戶與郵件服務器的連接
*
* @param host 郵件主機名
* @param username 發件人的用戶名
* @param password 發件人的用戶名密碼
* @return 返回指定用戶與指定郵件服務器綁定的一個連接(會話)
*/
public static Session getSession(String host, final String username,
final String password) {
// 設置配置文件,郵件主機和是否認證
Properties property = new Properties();
property.put("mail.host", host);
property.put("mail.smtp.auth", "true");
// 設置用戶名和密碼
Authenticator auth = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// TODO Auto-generated method stub
return new PasswordAuthentication(username, password);
}
};
// 獲取與郵件主機的連接
Session session = Session.getInstance(property, auth);
return session;
}
示例5: getMailSession
import javax.mail.Authenticator; //導入依賴的package包/類
private Session getMailSession(){
if(session==null){
Properties properties = new Properties();
properties.put("mail.smtp.host",smtpHost);
if(StringUtils.isNotEmpty(smtpPort)){
properties.put("mail.smtp.port",Integer.parseInt(smtpPort));
}
if(smtpIsAuth) {
properties.put("mail.smtp.auth","true");
session = Session.getDefaultInstance(properties,
new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(smtpUser,smtpPassword);
}
});
} else {
session = Session.getDefaultInstance(properties);
}
}
return session;
}
示例6: authorizeWebShopEmail
import javax.mail.Authenticator; //導入依賴的package包/類
public static Session authorizeWebShopEmail() throws MessagingException {
Session session = null;
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties props = System.getProperties();
props.setProperty("mail.smtp.host", "smtp.gmail.com");
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.store.protocol", "pop3");
props.put("mail.transport.protocol", "smtp");
final String username = ApplicationProperties.SHOP_EMAIL;
final String password = ApplicationProperties.SHOP_EMAIL_PASSWORD;
session = Session.getDefaultInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
return session;
}
示例7: sendMail
import javax.mail.Authenticator; //導入依賴的package包/類
/**
* 發送郵件 (暫時隻支持163郵箱發送)
* @param fromEmail 發送郵箱
* @param toEmail 接收郵箱
* @param emailName 163郵箱登錄名
* @param emailPassword 密碼
* @param title 發送主題
* @param centent 發送內容
* @throws Exception
*/
public void sendMail(String fromEmail,String toEmail,String emailName,String emailPassword,String title, String centent) throws Exception {
Properties properties = new Properties();// 創建Properties對象
properties.setProperty("mail.transport.protocol", appConfig.getValue("mail.transport.protocol"));// 設置傳輸協議
properties.put("mail.smtp.host", appConfig.getValue("mail.SMTPHost"));// 設置發信郵箱的smtp地址
properties.setProperty("mail.smtp.auth", "true"); // 驗證
Authenticator auth = new AjavaAuthenticator(emailName,
emailPassword); // 使用驗證,創建一個Authenticator
Session session = Session.getDefaultInstance(properties, auth);// 根據Properties,Authenticator創建Session
Message message = new MimeMessage(session);// Message存儲發送的電子郵件信息
message.setFrom(new InternetAddress(fromEmail));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(
toEmail));// 設置收信郵箱
// 指定郵箱內容及ContentType和編碼方式
message.setContent(centent, "text/html;charset=utf-8");
message.setSubject(title);// 設置主題
message.setSentDate(new Date());// 設置發信時間
Transport.send(message);// 發送
}
示例8: getConnection
import javax.mail.Authenticator; //導入依賴的package包/類
/**
* Getting the connection to email using Mail API
*
* @return - Email message Store
* @throws ESBMailTransportIntegrationTestException - Is thrown if an error while connecting to email store
*/
private static Store getConnection() throws ESBMailTransportIntegrationTestException {
Properties properties;
Session session;
properties = new Properties();
properties.setProperty("mail.host", "imap.gmail.com");
properties.setProperty("mail.port", "995");
properties.setProperty("mail.transport.protocol", "imaps");
session = Session.getInstance(properties, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(receiver + "@" + domain,
String.valueOf(receiverPassword));
}
});
try {
Store store = session.getStore("imaps");
store.connect();
return store;
} catch (MessagingException e) {
log.error("Error when creating the email store ", e);
throw new ESBMailTransportIntegrationTestException("Error when creating the email store ", e);
}
}
示例9: getSession
import javax.mail.Authenticator; //導入依賴的package包/類
public static Session getSession() {
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", "smtp.163.com");
props.setProperty("mail.smtp.port", "25");
props.setProperty("mail.smtp.auth", "true");
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
String password = null;
InputStream is = EmailUtils.class.getResourceAsStream("password.dat");
byte[] b = new byte[1024];
try {
int len = is.read(b);
password = new String(b,0,len);
} catch (IOException e) {
e.printStackTrace();
}
return new PasswordAuthentication(FROM, password);
}
});
return session;
}
示例10: sendEmail
import javax.mail.Authenticator; //導入依賴的package包/類
private void sendEmail(JSONObject msg, String type){
try{
Properties props = new Properties();
Authenticator auth = new EmailAutherticator(senderUsername,senderPassword);
props.put("mail.smtp.host", mailHost);
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, auth);
MimeMessage message = new MimeMessage(session);
Address address = new InternetAddress(senderUsername);
message.setFrom(address);
message.setSubject("dChat SDK Server [ "+type+" ] Mail");
message.setText(msg.toString(), "UTF-8");
message.setHeader("dChat SDK Server", type);
message.setSentDate(new Date());
message.addRecipients(Message.RecipientType.TO, new Address[]{address});
Transport.send(message);
}catch(Exception e){
LOG.error("send notifier email failed! "+e.getMessage(), e);
}
LOG.info("Send Exception/Error Notifier Email to Admin Success!");
}
示例11: getSession
import javax.mail.Authenticator; //導入依賴的package包/類
/**
* ��ȡ���ڷ����ʼ���Session
* @return
*/
public static Session getSession() {
Properties props = new Properties();
props.put("mail.smtp.host", HOST);//���÷�������ַ
props.put("mail.store.protocol" , PROTOCOL);//������
props.put("mail.smtp.port", PORT);//���ö˿�
props.put("mail.smtp.auth" , true);
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(SENDER, SENDERPWD);
}
};
Session session = Session.getDefaultInstance(props,authenticator);
return session;
}
示例12: getMailSession
import javax.mail.Authenticator; //導入依賴的package包/類
private Session getMailSession() {
Properties properties = new Properties();
properties.put("mail.smtp.auth", propertyService.getString("mail.smtp.auth"));
properties.put("mail.smtp.starttls.enable", propertyService.getString("mail.smtp.starttls.enable"));
properties.put("mail.smtp.host", propertyService.getString("mail.smtp.host"));
properties.put("mail.smtp.port", propertyService.getString("mail.smtp.port"));
Session session = Session.getInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(propertyService.getString("mail.userName"), propertyService.getString("mail.password"));
}
});
return session;
}
示例13: send
import javax.mail.Authenticator; //導入依賴的package包/類
/**
* 此段代碼用來發送普通電子郵件
*
* @throws MessagingException
* @throws UnsupportedEncodingException
* @throws UnsupportedEncodingException
*/
public void send() throws MessagingException, UnsupportedEncodingException {
Properties props = new Properties();
Authenticator auth = new Email_Autherticator(); // 進行郵件服務器用戶認證
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, auth);
// 設置session,和郵件服務器進行通訊。
MimeMessage message = new MimeMessage(session);
// message.setContent("foobar, "application/x-foobar"); // 設置郵件格式
message.setSubject(mail_subject); // 設置郵件主題
message.setText(mail_body); // 設置郵件正文
message.setHeader(mail_head_name, mail_head_value); // 設置郵件標題
message.setSentDate(new Date()); // 設置郵件發送日期
Address address = new InternetAddress(mail_from, personalName);
message.setFrom(address); // 設置郵件發送者的地址
Address toAddress = new InternetAddress(mail_to); // 設置郵件接收方的地址
message.addRecipient(Message.RecipientType.TO, toAddress);
Transport.send(message); // 發送郵件
}
示例14: build
import javax.mail.Authenticator; //導入依賴的package包/類
@Override
public Authenticator build() {
PropertyResolver propertyResolver = parent.environment().build();
if (updatable) {
if (!usernames.isEmpty() && !passwords.isEmpty()) {
return new UpdatableUsernamePasswordAuthenticator(propertyResolver, usernames, passwords);
}
return null;
}
String username = BuilderUtils.evaluate(this.usernames, propertyResolver, String.class);
String password = BuilderUtils.evaluate(this.passwords, propertyResolver, String.class);
if (username != null && password != null) {
return new UsernamePasswordAuthenticator(username, password);
}
return null;
}
示例15: createResourceImpl
import javax.mail.Authenticator; //導入依賴的package包/類
/**
* Creates and returns the resource.
*
* @return The resource
*/
@Override
protected MailResourcesHolder createResourceImpl()
{
//create authenticator
Authenticator authenticator=null;
if(this.password!=null)
{
authenticator=new MailAuthenticator(this.userName,this.password);
}
//create session
Session session=Session.getInstance(this.mailConnectionProperties,authenticator);
//create transport
Transport transport=this.createTransport(session);
//create holder
MailResourcesHolder resource=new MailResourcesHolder(session,transport);
return resource;
}