新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
在Java中使用Gmail發(fā)送郵件
以下Java代碼可以實(shí)現(xiàn)使用SMTP登陸到Gmail中并使用Gmail發(fā)送郵件。

為武穴等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及武穴網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都做網(wǎng)站、成都網(wǎng)站建設(shè)、武穴網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
使用Gmail發(fā)送郵件的代碼:
- String host = "smtp.gmail.com";
- String from = "username";
- String pass = "password";
- Properties props = System.getProperties();
- props.put("mail.smtp.starttls.enable", "true"); // 在本行添加
- 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");
- String[] to = {"[email protected]"}; // 在本行添加
- Session session = Session.getDefaultInstance(props, null);
- MimeMessage message = new MimeMessage(session);
- message.setFrom(new InternetAddress(from));
- InternetAddress[] toAddress = new InternetAddress[to.length];
- // 獲取地址的array
- for( int i=0; i < to.length; i++ ) { // 從while循環(huán)更改而成
- toAddress[i] = new InternetAddress(to[i]);
- }
- System.out.println(Message.RecipientType.TO);
- for( int i=0; i < toAddress.length; i++) { // 從while循環(huán)更改而成
- message.addRecipient(Message.RecipientType.TO, toAddress[i]);
- }
- message.setSubject("sending in a group");
- message.setText("Welcome to JavaMail");
- Transport transport = session.getTransport("smtp");
- transport.connect(host, from, pass);
- transport.sendMessage(message, message.getAllRecipients());
- transport.close();
代碼本身應(yīng)該很清楚了。在第7和8行加入你的Google賬號(hào)密碼:
- props.put("mail.smtp.user", from);
- props.put("mail.smtp.password", pass);
在12行加入收件人信息:
- String[] to = {"[email protected]"}; // 在本行添加
這樣就可以在你的Java程序中使用Gmail發(fā)送郵件啦。
網(wǎng)站欄目:在Java中使用Gmail發(fā)送郵件
URL地址:http://m.fisionsoft.com.cn/article/cohdigs.html


咨詢
建站咨詢
