Skip to content

Commit 62733d2

Browse files
Update GMail.java
1 parent cec90a8 commit 62733d2

File tree

1 file changed

+34
-56
lines changed

1 file changed

+34
-56
lines changed

GMail.java

+34-56
Original file line numberDiff line numberDiff line change
@@ -49,84 +49,62 @@ protected PasswordAuthentication getPasswordAuthentication() {
4949

5050
/**
5151
* @param subject The subject of the E-mail
52-
* Displays MessagingException on error
52+
* @throws MessagingException
5353
*/
54-
public void setSubject(String subject){
55-
try {
56-
msg.setSubject(subject);
57-
} catch (MessagingException e) {
58-
e.printStackTrace();
59-
}
54+
public void setSubject(String subject)throws MessagingException{
55+
msg.setSubject(subject);
6056
}
6157

6258
/**
6359
* @param s The Body text to be added in the E-mail
64-
* Displays MessagingException on error
60+
* @throws MessagingException
6561
*/
66-
public void addText(String s){
67-
try {
68-
MimeBodyPart mimeBodyPart=new MimeBodyPart();
69-
mimeBodyPart.setText(s);
70-
multipart.addBodyPart(mimeBodyPart);
71-
} catch (MessagingException e) {
72-
e.printStackTrace();
73-
}
62+
public void addText(String s)throws MessagingException{
63+
MimeBodyPart mimeBodyPart=new MimeBodyPart();
64+
mimeBodyPart.setText(s);
65+
multipart.addBodyPart(mimeBodyPart);
7466
}
7567

7668
/**
7769
* @param file The file to be attached in the E-mail in the form of File object
78-
* Displays MessagingException on error
79-
* Displays IOException if file not found
70+
* @throws MessagingException, IOException
8071
*/
81-
public void addAttachment(File file){
82-
try {
83-
MimeBodyPart mimeBodyPart=new MimeBodyPart();
84-
mimeBodyPart.attachFile(file);
85-
multipart.addBodyPart(mimeBodyPart);
86-
} catch (IOException | MessagingException e) {
87-
e.printStackTrace();
88-
}
72+
public void addAttachment(File file) throws MessagingException, IOException{
73+
MimeBodyPart mimeBodyPart=new MimeBodyPart();
74+
mimeBodyPart.attachFile(file);
75+
multipart.addBodyPart(mimeBodyPart);
8976
}
9077

9178
/**
9279
* @param fileAddress The absolute path of the file to be attached in the E-mail
93-
* Displays MessagingException on error
94-
* Displays IOException if file not found
80+
* @throws MessagingException, IOException
9581
*/
96-
public void addAttachment(String fileAddress){
97-
try {
98-
MimeBodyPart mimeBodyPart=new MimeBodyPart();
99-
mimeBodyPart.attachFile(fileAddress);
100-
multipart.addBodyPart(mimeBodyPart);
101-
} catch (IOException | MessagingException e) {
102-
e.printStackTrace();
103-
}
82+
public void addAttachment(String fileAddress) throws MessagingException, IOException{
83+
MimeBodyPart mimeBodyPart=new MimeBodyPart();
84+
mimeBodyPart.attachFile(fileAddress);
85+
multipart.addBodyPart(mimeBodyPart);
10486
}
10587

10688
/**
10789
* Method to send the message
10890
* @return true if the message is sent successfully or false if the message is not sent successfully
91+
* @throws MessagingException
10992
*/
110-
public boolean send(){
111-
try {
112-
msg.setFrom(new InternetAddress(fromAddress));
113-
Message.RecipientType type;
114-
switch (recipientType.toUpperCase()){
115-
case "TO": type=Message.RecipientType.TO; break;
116-
case "BCC": type=Message.RecipientType.BCC; break;
117-
case "CC": type=Message.RecipientType.CC;break;
118-
default: throw new IllegalStateException("Unexpected value: " + recipientType);
119-
}
120-
for(String to:toAddress)
121-
msg.addRecipient(type,new InternetAddress(to));
122-
MimeBodyPart mimeBodyPart=new MimeBodyPart();
123-
mimeBodyPart.setText("");
124-
multipart.addBodyPart(mimeBodyPart);
125-
msg.setContent(multipart);
126-
Transport.send(msg);
127-
return true;
128-
} catch (MessagingException e) {
129-
return false;
93+
public void send() throws MessagingException{
94+
msg.setFrom(new InternetAddress(fromAddress));
95+
Message.RecipientType type;
96+
switch (recipientType.toUpperCase()){
97+
case "TO": type=Message.RecipientType.TO; break;
98+
case "BCC": type=Message.RecipientType.BCC; break;
99+
case "CC": type=Message.RecipientType.CC;break;
100+
default: throw new IllegalStateException("Unexpected value: " + recipientType);
130101
}
102+
for(String to:toAddress)
103+
msg.addRecipient(type,new InternetAddress(to));
104+
MimeBodyPart mimeBodyPart=new MimeBodyPart();
105+
mimeBodyPart.setText("");
106+
multipart.addBodyPart(mimeBodyPart);
107+
msg.setContent(multipart);
108+
Transport.send(msg);
131109
}
132110
}

0 commit comments

Comments
 (0)