本文整理汇总了Java中com.google.api.services.gmail.Gmail类的典型用法代码示例。如果您正苦于以下问题:Java Gmail类的具体用法?Java Gmail怎么用?Java Gmail使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Gmail类属于com.google.api.services.gmail包,在下文中一共展示了Gmail类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: performRequest
import com.google.api.services.gmail.Gmail; //导入依赖的package包/类
private static void performRequest(String accessToken, String refreshToken, String apiKey, String apiSecret) throws GeneralSecurityException, IOException, MessagingException {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = new JacksonFactory();
final Credential credential = convertToGoogleCredential(accessToken, refreshToken, apiSecret, apiKey);
Builder builder = new Gmail.Builder(httpTransport, jsonFactory, credential);
builder.setApplicationName("OAuth API Sample");
Gmail gmail = builder.build();
MimeMessage content = createEmail("[email protected]", "[email protected]", "Test Email", "It works");
Message message = createMessageWithEmail(content);
gmail.users().messages().send("[email protected]", message).execute();
}
示例2: makeClient
import com.google.api.services.gmail.Gmail; //导入依赖的package包/类
@Override
public Gmail makeClient(String clientId, String clientSecret, Collection<String> scopes, String applicationName, String refreshToken, String accessToken) {
Credential credential;
try {
credential = authorize(clientId, clientSecret, scopes);
if (refreshToken != null && !"".equals(refreshToken)) {
credential.setRefreshToken(refreshToken);
}
if (accessToken != null && !"".equals(accessToken)) {
credential.setAccessToken(accessToken);
}
return new Gmail.Builder(transport, jsonFactory, credential).setApplicationName(applicationName).build();
} catch (Exception e) {
LOG.error("Could not create Google Drive client.", e);
}
return null;
}
示例3: getGmailService
import com.google.api.services.gmail.Gmail; //导入依赖的package包/类
private static Gmail getGmailService(String basedir, String appName) throws Exception {
// 機密情報ファイルのパス
File DATA_STORE_DIR = new java.io.File(basedir, "gmail-secrets");
File SECRET_JSON = new java.io.File(DATA_STORE_DIR, "client_secret.json");
// 準備
FileDataStoreFactory DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
HttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
// 送信のみ
List<String> SCOPES = Arrays.asList(GmailScopes.GMAIL_SEND);
// Credential取得
try (InputStream in = FileUtils.openInputStream(SECRET_JSON)) {
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY,
clientSecrets, SCOPES).setDataStoreFactory(DATA_STORE_FACTORY).setAccessType("offline").build();
Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
// Gmailインスタンス生成
return new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName(appName).build();
}
}
示例4: startAuthentication
import com.google.api.services.gmail.Gmail; //导入依赖的package包/类
/**
* Build and return an authorized Gmail client service.
*
* @return an authorized Gmail client service
* @throws IOException
*/
public static void startAuthentication() throws IOException {
Credential credential = authorize();
service = new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
}
示例5: checkGmailApiRequirements
import com.google.api.services.gmail.Gmail; //导入依赖的package包/类
private void checkGmailApiRequirements() {
String accountName = PreferenceManager.getDefaultSharedPreferences(getContext())
.getString(PREF_ACCOUNT_NAME, null);
if (accountName != null) {
GoogleAccountCredential mCredential = GoogleAccountCredential.usingOAuth2(
getContext().getApplicationContext(), Arrays.asList(SCOPES))
.setBackOff(new ExponentialBackOff());
mCredential.setSelectedAccountName(accountName);
if (!DeviceUtils.isGooglePlayServicesAvailable(getContext())) {
DeviceUtils.acquireGooglePlayServices(getContext());
}
else{
mService = new Gmail.Builder(
AndroidHttp.newCompatibleTransport(), JacksonFactory.getDefaultInstance(), mCredential)
.setApplicationName(AppUtils.getApplicationName(getContext()))
.build();
authorized = true;
}
} else {
GmailAuthorizationActivity.setListener(this);
getContext().startActivity(new Intent(getContext(), GmailAuthorizationActivity.class));
}
}
示例6: sendGmailinBackground
import com.google.api.services.gmail.Gmail; //导入依赖的package包/类
public sendGmailinBackground(GoogleAccountCredential credential,int order){
isSending=true;
HttpTransport transport = AndroidHttp.newCompatibleTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
mService = new Gmail.Builder(
transport, jsonFactory, credential)
.setApplicationName(getActivity().getString(R.string.app_name))
.build();
this.order = order;
Log.d("Email","Sending Created!");
}
示例7: MakeRequestTask
import com.google.api.services.gmail.Gmail; //导入依赖的package包/类
MakeRequestTask(MainActivity activity, GoogleAccountCredential credential) {
HttpTransport transport = AndroidHttp.newCompatibleTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
mService = new com.google.api.services.gmail.Gmail.Builder(
transport, jsonFactory, credential)
.setApplicationName(getResources().getString(R.string.app_name))
.build();
this.activity = activity;
}
示例8: sendMessage
import com.google.api.services.gmail.Gmail; //导入依赖的package包/类
private String sendMessage(Gmail service,
String userId,
MimeMessage email)
throws MessagingException, IOException {
Message message = createMessageWithEmail(email);
// GMail's official method to send email with oauth2.0
message = service.users().messages().send(userId, message).execute();
System.out.println("Message id: " + message.getId());
System.out.println(message.toPrettyString());
return message.getId();
}
示例9: getClient
import com.google.api.services.gmail.Gmail; //导入依赖的package包/类
public Gmail getClient() {
if (client == null) {
client = getClientFactory().makeClient(configuration.getClientId(), configuration.getClientSecret(), configuration.getScopes(), configuration.getApplicationName(),
configuration.getRefreshToken(), configuration.getAccessToken());
}
return client;
}
示例10: getGmailService
import com.google.api.services.gmail.Gmail; //导入依赖的package包/类
/**
* Build and return an authorized Gmail client service.
* @return an authorized Gmail client service
* @throws IOException
*/
private static Gmail getGmailService() throws IOException {
Credential credential = authorize();
return new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
}
示例11: sendEmail
import com.google.api.services.gmail.Gmail; //导入依赖的package包/类
public static void sendEmail(String to, String subject, String message){
// Build a new authorized API client service.
Gmail service;
try {
service = getGmailService();
// Print the labels in the user's account.
String user = "me";
try {
MimeMessage mm = createEmail(to, "[email protected]", subject,
message);
sendMessage(service,user,mm);
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
示例12: main
import com.google.api.services.gmail.Gmail; //导入依赖的package包/类
public static void main(String[] args) throws IOException {
// Build a new authorized API client service.
Gmail service = getGmailService();
// Print the labels in the user's account.
String user = "me";
try {
MimeMessage mm = createEmail("[email protected]", "[email protected]", "Subject",
"The body of the message");
sendMessage(service,user,mm);
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*
ListLabelsResponse listResponse =
service.users().labels().list(user).execute();
List<Label> labels = listResponse.getLabels();
if (labels.size() == 0) {
System.out.println("No labels found.");
} else {
System.out.println("Labels:");
for (Label label : labels) {
System.out.printf("- %s\n", label.getName());
}
}
*/
}
示例13: updateCredential
import com.google.api.services.gmail.Gmail; //导入依赖的package包/类
public void updateCredential(SharedPreferences prefs){
credential = GoogleAccountCredential.usingOAuth2(
getApplicationContext(), Arrays.asList(GMConstants.SCOPES))
.setBackOff(new ExponentialBackOff())
.setSelectedAccountName(prefs.getString(GMConstants.PREF_ACCOUNT_NAME, null));
if(credential != null && credential.getSelectedAccount() != null) {
gmailService = new com.google.api.services.gmail.Gmail.Builder(
transport, jsonFactory, credential)
.setApplicationName("Shortyz")
.build();
} else {
gmailService = null;
}
}
示例14: createGmailService
import com.google.api.services.gmail.Gmail; //导入依赖的package包/类
public static Gmail createGmailService(final HttpTransport httpTransport,
final JsonFactory jsonFactory,
final AppCode appCode,
final GoogleCredential credential) {
// Create a new authorized Gmail API client
Gmail gmailService = new Gmail.Builder(httpTransport,
jsonFactory,
credential)
.setApplicationName(appCode.asString())
.build();
return gmailService;
}
示例15: _sendMessage
import com.google.api.services.gmail.Gmail; //导入依赖的package包/类
private static void _sendMessage(final Gmail service,
final String userId,
final MimeMessage email) throws MessagingException,
IOException {
Message message = _createMessageWithEmail(email);
message = service.users()
.messages()
.send(userId,message)
.execute();
System.out.println("Message id: " + message.getId());
System.out.println(message.toPrettyString());
}