How to declare mail id in java
In Java, you can declare a mail ID (also known as an email address) as a string variable. Here's an example:
String mailId = "[email protected]";
Replace your_email
with the actual email address you want to use.
Alternatively, you can use a java.lang.String
object to declare a mail ID:
String mailId = new String("[email protected]");
Note that in both cases, the mail ID is a string, so you can use string manipulation methods like substring()
, indexOf()
, etc. on it.
If you want to validate the email address using a regular expression, you can use a Pattern
and Matcher
objects:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
String mailId = "[email protected]";
Pattern pattern = Pattern.compile("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$");
Matcher matcher = pattern.matcher(mailId);
if (matcher.matches()) {
System.out.println("Valid email address");
} else {
System.out.println("Invalid email address");
}
This regular expression pattern matches most common email address formats, but keep in mind that it's not foolproof and may not match all possible valid email addresses.
In Java, you can also use the java.mail
package to send emails, but that's a more advanced topic.