How can be sent an auto-generated email to a user when some other user invites him on my website “by isuing the java code”
-
18-06-2021 - |
Question
I have a problem; I want to write a java code for sending an automatic email to a registered user on my website when some other user invites him. Then the invitee will accept or reject the invitation inside the email. Please guide me...........
Solution
Let him click a link in that email.
Email code:
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
public void sendEmail(String aFromEmailAddr, String aToEmailAddr,
String aSubject, String aBody){
//Here, no Authenticator argument is used (it is null).
//Authenticators are used to prompt the user for user
//name and password.
Session session = Session.getDefaultInstance( fMailServerConfig, null );
MimeMessage message = new MimeMessage( session );
try {
//the "from" address may be set in code, or set in the
//config file under "mail.from" ; here, the latter style is used
//message.setFrom( new InternetAddress(aFromEmailAddr) );
message.addRecipient(
Message.RecipientType.TO, new InternetAddress(aToEmailAddr)
);
message.setSubject( aSubject );
message.setText( aBody );
Transport.send( message );
}
catch (MessagingException ex){
System.err.println("Cannot send email. " + ex);
}
}
And you can put a link in that email. I believe basic HTML is supported in mail clients, so you can do something like:
Hi PersonWithANameEnteredByTheGuyWhoInvitedYou,
NameOfTheGuyWhoInvitedYou invited you, click here to accept his invitation:
<a href="http://mydomain.com/accept?param=ridiculousLongTokenToVerifyTheRequestAndIdentifyThePersonThatClickedIt">ACCEPT!</a>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow