各位大神們要請問
我想設定用 GMAIL SMTP 做聯絡我們
但不知道為什麼出錯了
錯誤訊息如下
SMTP Error: Could not connect to SMTP host. 發送錯誤: SMTP Error: Could not connect to SMTP host.
我自已寫的語法如下
<?php
include("class.phpmailer.php"); //匯入PHPMailer類別
$Name=$_POST['sndname'];
$Mail=$_POST['sendmail'];
$Subject=$_POST['subject'];
$Sendbody=$_POST['sendbody'];
$mail= new PHPMailer(); //建立新物件
$mail->IsSMTP(); //設定使用SMTP方式寄信
$mail->SMTPAuth = true; //設定SMTP需要驗證
$mail->SMTPSecure = "ssl"; // Gmail的SMTP主機需要使用SSL連線
$mail->Host = "smtp.gmail.com"; //Gamil的SMTP主機
$mail->Port = 465; //Gamil的SMTP主機的埠號(Gmail為465)。
$mail->CharSet = "utf-8"; //郵件編碼
$mail->Username = "xxx@gmail.com"; //Gamil帳號
$mail->Password = "xxxxxxxxxxxxxxxxxxxx"; //Gmail密碼
$mail->From = $Mail; //寄件者信箱
$mail->FromName = "線上客服"; //寄件者姓名
$mail->Subject ="一封線上客服信"; //郵件標題
$mail->Body = "姓名:".$Name."<br>信箱:".$Mail."<br>主題:".$Subject."<br>回應內容:".$Sendbody; //郵件內容
$mail->IsHTML(true); //郵件內容為html ( true || false)
$mail->AddAddress("xxx@gmail.com"); //收件者郵件及名稱
if(!$mail->Send()) {
echo "發送錯誤: " . $mail->ErrorInfo;
} else {
echo "<div align=center>感謝您的回覆,我們將會盡速處理!</div>";
}
?>
你gmail有設定低安全性嗎?
public class JMail {
public static Tool jtool = javaapplication4.JavaApplication4.jtool;
static String host = "smtp.gmail.com";
static int port = 587;
static final String username = Gmail_user;
static final String password = Gmail_password;
public static void Send_mail(String To_mail, String Mail_title, String Mail_text) {
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.port", port);
Session session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(To_mail));
message.setSubject(Mail_title);
message.setText(Mail_text);
Transport transport = session.getTransport("smtp");
transport.connect(host, port, username, password);
Transport.send(message);
System.out.println(jtool.getDateTime(1) + " " + To_mail + " " + Mail_title + " " + "寄送email結束.");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
我是用587,JAVA寫的,請參考