本文整理汇总了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;
}