我是Java編程的初學者。使用JavaMail API,我編寫了一個程序來發送電子郵件。現在我需要創建一個前端並連接它們。我只用記事本編寫程序,我不使用任何IDE。如何輕鬆創建前端並連接到我的程序?創建前端並用Java連接它們
我的計劃是:
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
import java.util.*;
public class Mailer {
public void Mailer() throws Exception {
String usrname;
String pwd;
Scanner in = new Scanner(System.in);
System.out.println("\nEnter the gmail user name :");
usrname = in.next();
System.out.println("\nEnter the Password :");
pwd = in.next();
String HOST_NAME = "smtp.gmail.com";
int HOST_PORT = 465;
Properties props = new Properties();
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtps.host", HOST_NAME);
props.put("mail.smtps.auth", "true");
Session mailSession = Session.getDefaultInstance(props);
Transport transport = mailSession.getTransport();
String toadd;
System.out.println("\nEnter the Recipient Address:");
toadd = in.next();
MimeMessage message = new MimeMessage(mailSession);
System.out.println("\nEnter the Subject:");
String sub = in.nextLine();
message.setSubject(sub);
System.out.println("\nEnter the message body:");
String body = in.nextLine();
message.setContent(body, "text/plain");
message.addRecipient(Message.RecipientType.TO, new InternetAddress(toadd));
transport.connect(HOST_NAME, HOST_PORT, usrname, pwd);
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
transport.close();
System.out.println("Mail Sent successfully!!!!");
System.exit(0);
}
public static void main(String[] args) throws Exception {
System.out.println("*******************Welcome to Mailer*************************");
Mailer mail = new Mailer();
mail.Mailer();
}
}
寫入的代碼不是以這樣的方式編寫的,即該類可以被GUI使用,因爲它使用諸如'Scanner'和'System.out'之類的東西。爲了可以通過GUI和命令行使用,發送電子郵件的過程需要被抽象爲由兩者調用的類(或方法)。 –
歡迎來到StackOverflow!如果你不使用IDE,你的開發將花費更長時間,並且很難(很多!)更難以調試和做對。我強烈建議您花時間學習Eclipse或IntelliJ IDEA社區版:它們都是免費的,不是很難使用(如果您不使用「高級功能」),並且會爲您節省大量的麻煩。使用記事本管理多個類是一場噩夢,我甚至不談論重構代碼 – Guillaume
對於GUI部分:請參閱swing教程:http://docs.oracle.com/javase/tutorial/uiswing/ – keuleJ